標籤:puts odi end pcl eth 亂碼 head 雙向 迴流
1.httpclient就是個能發送http串連的工具包,包括能發送post請求和get請求。
http 串連一次就有返迴流。http是個雙向的嘛。只有串連了,就會有輸出返迴流。
所以在執行http串連的時候,傳回值都是http串連的返迴流。
HttpResponse response = client.execute(httpPost);
2.http發送,body裡是可以寫入中文的。但要注意亂碼問題:
- public static String getHttpRequestString(String url,String body) throws IOException {
- HttpClient client = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost(url);
-
- StringEntity stringEntity = new StringEntity(body);
- httpPost.setEntity(stringEntity);
- httpPost.setHeader("Content-Type", "application/json; charset=UTF-8");
-
- HttpResponse response = client.execute(httpPost);
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
- String line;
- StringBuffer jsonString = new StringBuffer();
- while((line = bufferedReader.readLine()) != null) {
- jsonString.append(line);
- }
- return jsonString.toString();
- }
這是最初的代碼,如果傳輸的body有中文漢字的話,如果對方設定的格式是UTF-8,那麼他接收到的字元是亂碼,
stringEntity.setContentEncoding("UTF-8");
加上這樣一句代碼,設定下格式就好了。
httpclient就是個能發送http串連的工具包,包括能發送post請求和get請求