關於HttpClient的總結

來源:互聯網
上載者:User

 關於Httpclient的使用總結如下:

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

  轉載請標明出處 http://blog.csdn.net/shimiso 

技術交流群:173711587

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.