httpclient就是個能發送http串連的工具包,包括能發送post請求和get請求

來源:互聯網
上載者:User

標籤:puts   odi   end   pcl   eth   亂碼   head   雙向   迴流   

1.httpclient就是個能發送http串連的工具包,包括能發送post請求和get請求。

http 串連一次就有返迴流。http是個雙向的嘛。只有串連了,就會有輸出返迴流。

所以在執行http串連的時候,傳回值都是http串連的返迴流。

HttpResponse response = client.execute(httpPost);

 

2.http發送,body裡是可以寫入中文的。但要注意亂碼問題:

  1. public static String getHttpRequestString(String url,String body) throws IOException {  
  2.         HttpClient client = new DefaultHttpClient();  
  3.         HttpPost httpPost = new HttpPost(url);  
  4.   
  5.         StringEntity stringEntity = new StringEntity(body);  
  6.         httpPost.setEntity(stringEntity);  
  7.         httpPost.setHeader("Content-Type", "application/json; charset=UTF-8");  
  8.   
  9.         HttpResponse response = client.execute(httpPost);  
  10.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));  
  11.         String line;  
  12.         StringBuffer jsonString = new StringBuffer();  
  13.         while((line = bufferedReader.readLine()) != null) {  
  14.             jsonString.append(line);  
  15.         }  
  16.         return jsonString.toString();  
  17.     }  

 

這是最初的代碼,如果傳輸的body有中文漢字的話,如果對方設定的格式是UTF-8,那麼他接收到的字元是亂碼,

 

stringEntity.setContentEncoding("UTF-8");

加上這樣一句代碼,設定下格式就好了。

httpclient就是個能發送http串連的工具包,包括能發送post請求和get請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.