關於Httpclient的使用總結如下:
- (1)當HttpClient的執行個體不再需要時,可以使用連線管理員關閉
- httpclient.getConnectionManager().shutdown();
(1)當HttpClient的執行個體不再需要時,可以使用連線管理員關閉httpclient.getConnectionManager().shutdown();
- (2)針對HTTPs的協議的HttpClient請求必須使用者和密碼
- httpclient.getCredentialsProvider()
- .setCredentials(new AuthScope("localhost", 443),
- new UsernamePasswordCredentials("username", "password"));
(2)針對HTTPs的協議的HttpClient請求必須使用者和密碼 httpclient.getCredentialsProvider() .setCredentials(new AuthScope("localhost", 443), new UsernamePasswordCredentials("username", "password"));
- (3)如果不想擷取HTTPClient返回的資訊
- httpclient.abort();
(3)如果不想擷取HTTPClient返回的資訊 httpclient.abort();
-
- (4)httpclient傳送檔案的方式
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost("http://www.apache.org");
- File file = new File(args[0]);
- InputStreamEntity reqEntity = new InputStreamEntity(
- new FileInputStream(file), -1);
- reqEntity.setContentType("binary/octet-stream");
- reqEntity.setChunked(true);
- // It may be more appropriate to use FileEntity class in this particular
- // instance but we are using a more generic InputStreamEntity to demonstrate
- // the capability to stream out data from any arbitrary source
- //
- // FileEntity entity = new FileEntity(file, "binary/octet-stream");
- httppost.setEntity(reqEntity);
- System.out.println("executing request " + httppost.getRequestLine());
- HttpResponse response = httpclient.execute(httppost);
(4)httpclient傳送檔案的方式 HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.apache.org"); File file = new File(args[0]); InputStreamEntity reqEntity = new InputStreamEntity( new FileInputStream(file), -1); reqEntity.setContentType("binary/octet-stream"); reqEntity.setChunked(true); // It may be more appropriate to use FileEntity class in this particular // instance but we are using a more generic InputStreamEntity to demonstrate // the capability to stream out data from any arbitrary source // // FileEntity entity = new FileEntity(file, "binary/octet-stream"); httppost.setEntity(reqEntity); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost);
- (5)擷取Cookie的資訊
- HttpClient httpclient = new DefaultHttpClient();
- // 建立一個本地Cookie儲存的執行個體
- CookieStore cookieStore = new BasicCookieStore();
- //建立一個本機內容資訊
- HttpContext localContext = new BasicHttpContext();
- //在本地上下問中綁定一個本機存放區
- localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
- //佈建要求的路徑
- HttpGet httpget = new HttpGet("http://www.google.com/");
- //傳遞本地的http上下文給伺服器
- HttpResponse response = httpclient.execute(httpget, localContext);
- //擷取本地資訊
- HttpEntity entity = response.getEntity();
- System.out.println(response.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
- }
- //擷取cookie中的各種資訊
- List<Cookie> cookies = cookieStore.getCookies();
- for (int i = 0; i < cookies.size(); i++) {
- System.out.println("Local cookie: " + cookies.get(i));
- }
- //擷取訊息頭的資訊
- Header[] headers = response.getAllHeaders();
- for (int i = 0; i<headers.length; i++) {
- System.out.println(headers[i]);
- }
(5)擷取Cookie的資訊 HttpClient httpclient = new DefaultHttpClient(); // 建立一個本地Cookie儲存的執行個體 CookieStore cookieStore = new BasicCookieStore(); //建立一個本機內容資訊 HttpContext localContext = new BasicHttpContext(); //在本地上下問中綁定一個本機存放區 localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); //佈建要求的路徑 HttpGet httpget = new HttpGet("http://www.google.com/"); //傳遞本地的http上下文給伺服器 HttpResponse response = httpclient.execute(httpget, localContext); //擷取本地資訊 HttpEntity entity = response.getEntity(); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } //擷取cookie中的各種資訊 List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { System.out.println("Local cookie: " + cookies.get(i)); } //擷取訊息頭的資訊 Header[] headers = response.getAllHeaders(); for (int i = 0; i<headers.length; i++) { System.out.println(headers[i]); }
- (6)針對典型的SSL請求的處理
- DefaultHttpClient httpclient = new DefaultHttpClient();
- //擷取預設的儲存體金鑰類
- KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
- //載入本地的密鑰資訊
- FileInputStream instream = new FileInputStream(new File("my.keystore"));
- try {
- trustStore.load(instream, "nopassword".toCharArray());
- } finally {
- instream.close();
- }
- //建立SSLSocketFactory,建立相關的Socket
- SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
- //設定協議的類型和密鑰資訊,以及斷開資訊
- Scheme sch = new Scheme("https", socketFactory, 443);
- //在連線管理員中註冊中資訊
- httpclient.getConnectionManager().getSchemeRegistry().register(sch);
(6)針對典型的SSL請求的處理 DefaultHttpClient httpclient = new DefaultHttpClient(); //擷取預設的儲存體金鑰類 KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); //載入本地的密鑰資訊 FileInputStream instream = new FileInputStream(new File("my.keystore")); try { trustStore.load(instream, "nopassword".toCharArray()); } finally { instream.close(); } //建立SSLSocketFactory,建立相關的Socket SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore); //設定協議的類型和密鑰資訊,以及斷開資訊 Scheme sch = new Scheme("https", socketFactory, 443); //在連線管理員中註冊中資訊 httpclient.getConnectionManager().getSchemeRegistry().register(sch);
- (7)佈建要求的參數的幾種方式
- A.在請求的路徑中以查詢字串格式傳遞參數
- B.在請求的實體中添加參數
- List <NameValuePair> nvps = new ArrayList <NameValuePair>();
- nvps.add(new BasicNameValuePair("IDToken1", "username"));
- nvps.add(new BasicNameValuePair("IDToken2", "password"));
- httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
轉載請標明出處 http://blog.csdn.net/shimiso
技術交流群:173711587