使用HttpClient,您可以連線到需要使用者名和密碼的網站。本章介紹如何針對要求輸入使用者名和密碼的站點執行用戶端請求。
第1步 - 建立CredentialsProvider物件CredentialsProvider
介面維護一個集合以儲存使用者登入憑據。可以通過範例化BasicCredentialsProvider
類(此介面的預設實現)來建立其物件。
CredentialsProvider credentialsPovider = new BasicCredentialsProvider();
第2步 - 設定憑據
可以使用setCredentials()
方法將所需憑據設定為CredentialsProvider
物件。
此方法接受以下給出的兩個物件 -
使用setCredentials()
方法為主機和代理設定憑據,如下所示 -
credsProvider.setCredentials(new AuthScope("example.com", 80),
new UsernamePasswordCredentials("user", "mypass"));
credsProvider.setCredentials(new AuthScope("localhost", 8000),
new UsernamePasswordCredentials("abc", "passwd"));
第3步 - 建立HttpClientBuilder物件
使用HttpClients類的custom()
方法建立HttpClientBuilder
。
//Creating the HttpClientBuilder
HttpClientBuilder clientbuilder = HttpClients.custom();
第4步 - 設定credentialsPovider
可以使用setDefaultCredentialsProvider()
方法將上面建立的credentialsPovider
物件設定為HttpClientBuilder
。
將上一步中建立的CredentialProvider
物件設定為用戶端構建器,方法是將其傳遞給CredentialsProvider object()方法,如下所示。
clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);
第5步 - 構建CloseableHttpClient
使用HttpClientBuilder
類的build()
方法構建CloseableHttpClient
物件。
CloseableHttpClient httpclient = clientbuilder.build();
第6步 - 建立一個HttpGet物件並執行它
通過範例化HttpGet
類來建立HttpRequest物件。使用execute()
方法執行此請求。
//Creating a HttpGet object
HttpGet httpget = new HttpGet("https://www.tw511.com/ ");
//Executing the Get request
HttpResponse httpresponse = httpclient.execute(httpget);
範例
以下是一個範例程式,演示了針對需要使用者身份驗證的目標站點執行HTTP請求。
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
public class UserAuthenticationExample {
public static void main(String args[]) throws Exception{
//Create an object of credentialsProvider
CredentialsProvider credentialsPovider = new BasicCredentialsProvider();
//Set the credentials
AuthScope scope = new AuthScope("https://www.kaops.com/", 80);
Credentials credentials = new UsernamePasswordCredentials("USERNAME", "PASSWORD");
credentialsPovider.setCredentials(scope,credentials);
//Creating the HttpClientBuilder
HttpClientBuilder clientbuilder = HttpClients.custom();
//Setting the credentials
clientbuilder = clientbuilder.setDefaultCredentialsProvider(credentialsPovider);
//Building the CloseableHttpClient object
CloseableHttpClient httpclient = clientbuilder.build();
//Creating a HttpGet object
HttpGet httpget = new HttpGet("https://www.kaops.com/index.php");
//Printing the method used
System.out.println(httpget.getMethod());
//Executing the Get request
HttpResponse httpresponse = httpclient.execute(httpget);
//Printing the status line
System.out.println(httpresponse.getStatusLine());
int statusCode = httpresponse.getStatusLine().getStatusCode();
System.out.println(statusCode);
Header[] headers= httpresponse.getAllHeaders();
for (int i = 0; i<headers.length;i++){
System.out.println(headers[i].getName());
}
}
}
執行上面範例程式碼,得到以下結果:
GET
HTTP/1.1 200 OK
200