httpclient post請求例子(無參數名與帶參數名的例子),多線程並發處理


版本:4.1

  • 帶參數名的情況
    HttpClient httpClient = new DefaultHttpClient(); 
HttpPost httpPost = new HttpPost(url); // httpPost.setHeader("Accept-Encoding", "gzip,deflate");//表示返回的數據是壓縮的zip格式 String postParam = "";//請求的參數內容 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("data", postParam)); httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8")); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if (response.getStatusLine().getStatusCode() == 200) {if (entity.getContentEncoding().toString().equalsIgnoreCase("Content-Encoding: gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); //對zip進行解壓 entity = response.getEntity(); } String responseContent = EntityUtils.toString(entity); System.out.println("responseContent: \n" + responseContent); } }
  • 無參數名的情況

 

   HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(url);
//  httpPost.setHeader("Accept-Encoding", "gzip,deflate");//表示返回的數據是壓縮的zip格式
    String postParam = "";//請求的參數內容 
    StringEntity paramEntity = new StringEntity(postParam);//無參數名,只是參數內容 httpPost.setEntity(paramEntity); 
    HttpResponse response = httpClient.execute(httpPost);    
    HttpEntity entity = response.getEntity();
    if (response.getStatusLine().getStatusCode() == 200) {if (entity.getContentEncoding().toString().equalsIgnoreCase("Content-Encoding: gzip")) {
                response.setEntity(new GzipDecompressingEntity(response.getEntity())); //對zip進行解壓
                entity = response.getEntity();
           }
           String responseContent = EntityUtils.toString(entity);
           System.out.println("responseContent: \n" + responseContent); 
   }
}


 

  • httpEntity的類結構圖

 

Httpclient並發處理處理:主要改變Httpclient對象的生成

    /** 
     * 適合多線程的HttpClient,用httpClient4.2.1實現 
     * @return DefaultHttpClient 
     */  
    public static DefaultHttpClient getHttpClient()  
    {         
        // 設置組件參數, HTTP協議的版本,1.1/1.0/0.9   
        HttpParams params = new BasicHttpParams();   
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);   
        HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");   
        HttpProtocolParams.setUseExpectContinue(params, true);      
      
        //設置連接超時時間   
        int REQUEST_TIMEOUT = 60*1000;  //設置請求超時60秒鍾   
        int SO_TIMEOUT = 60*1000;       //設置等待數據超時時間60秒鍾   
        //HttpConnectionParams.setConnectionTimeout(params, REQUEST_TIMEOUT);  
        //HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);  
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, REQUEST_TIMEOUT);    
        params.setParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT);   
        
        //設置訪問協議   
        SchemeRegistry schreg = new SchemeRegistry();    
        schreg.register(new Scheme("http",80,PlainSocketFactory.getSocketFactory()));   
        schreg.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));         
          
        //多連接的線程安全的管理器   
        PoolingClientConnectionManager pccm = new PoolingClientConnectionManager(schreg);  
        pccm.setDefaultMaxPerRoute(20); //每個主機的最大並行鏈接數   
        pccm.setMaxTotal(100);          //客戶端總並行鏈接最大數      
          
        DefaultHttpClient httpClient = new DefaultHttpClient(pccm, params);    
        return httpClient;  
    }  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM