用httpclient4.x 发送http get post请求 中文编码问题

  1. <pre name="code" class="java">package org.ibmp.gmp.util;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.UnsupportedEncodingException;  
  5. import java.net.URI;  
  6. import java.net.URISyntaxException;  
  7. import java.net.URLEncoder;  
  8. import java.text.SimpleDateFormat;  
  9. import java.util.ArrayList;  
  10. import java.util.Date;  
  11. import java.util.List;  
  12.   
  13. import org.apache.http.HttpEntity;  
  14. import org.apache.http.HttpResponse;  
  15. import org.apache.http.NameValuePair;  
  16. import org.apache.http.ParseException;  
  17. import org.apache.http.client.ClientProtocolException;  
  18. import org.apache.http.client.HttpClient;  
  19. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  20. import org.apache.http.client.methods.HttpGet;  
  21. import org.apache.http.client.methods.HttpPost;  
  22. import org.apache.http.entity.StringEntity;  
  23. import org.apache.http.impl.client.DefaultHttpClient;  
  24. import org.apache.http.message.BasicNameValuePair;  
  25. import org.apache.http.protocol.HTTP;  
  26. import org.apache.http.util.EntityUtils;  
  27.   
  28. public class HttpClientUtil {  
  29.   
  30.     private static HttpClient httpClient = new DefaultHttpClient();  
  31.   
  32.     /**  
  33.      * 发送Get请求  
  34.      * @param url  
  35.      * @param params  
  36.      * @return  
  37.      */  
  38.     public static String get(String url, List<NameValuePair> params) {  
  39.         String body = null;  
  40.         try {  
  41.             // Get请求  
  42.             HttpGet httpget = new HttpGet(url);  
  43.             // 设置参数  
  44.             String str = EntityUtils.toString(new UrlEncodedFormEntity(params));  
  45.             httpget.setURI(new URI(httpget.getURI().toString() + "?" + str));  
  46.             // 发送请求  
  47.             HttpResponse httpresponse = httpClient.execute(httpget);  
  48.             // 获取返回数据  
  49.             HttpEntity entity = httpresponse.getEntity();  
  50.             body = EntityUtils.toString(entity);  
  51.             if (entity != null) {  
  52.                 entity.consumeContent();  
  53.             }  
  54.         } catch (ParseException e) {  
  55.             e.printStackTrace();  
  56.         } catch (UnsupportedEncodingException e) {  
  57.             e.printStackTrace();  
  58.         } catch (IOException e) {  
  59.             e.printStackTrace();  
  60.         } catch (URISyntaxException e) {  
  61.             e.printStackTrace();  
  62.         }  
  63.         return body;  
  64.     }  
  65.   
  66.     /**  
  67.      * 发送 Post请求  
  68.      * @param url  
  69.      * @param reqXml  
  70.      * @return  
  71.      */  
  72.     public static String post(String url, String reqXml) {  
  73.         String body = null;  
  74.         try {  
  75.             //设置客户端编码  
  76.             if (httpClient == null) {  
  77.                 // Create HttpClient Object  
  78.                 httpClient = new DefaultHttpClient();  
  79.                 }  
  80.             httpClient.getParams().setParameter("http.protocol.content-charset",HTTP.UTF_8);  
  81.             httpClient.getParams().setParameter(HTTP.CONTENT_ENCODING, HTTP.UTF_8);  
  82.             httpClient.getParams().setParameter(HTTP.CHARSET_PARAM, HTTP.UTF_8);  
  83.             httpClient.getParams().setParameter(HTTP.DEFAULT_PROTOCOL_CHARSET,HTTP.UTF_8);  
  84.             // Post请求  
  85.             HttpPost httppost = new HttpPost(url);  
  86.             //设置post编码  
  87.             httppost.getParams().setParameter("http.protocol.content-charset",HTTP.UTF_8);  
  88.             httppost.getParams().setParameter(HTTP.CONTENT_ENCODING, HTTP.UTF_8);  
  89.             httppost.getParams().setParameter(HTTP.CHARSET_PARAM, HTTP.UTF_8);  
  90.             httppost.getParams().setParameter(HTTP.DEFAULT_PROTOCOL_CHARSET, HTTP.UTF_8);  
  91.       
  92.             // 设置参数  
  93.              List<NameValuePair> params = new ArrayList<NameValuePair>();  
  94.              params.add(new BasicNameValuePair("$xmldata", reqXml));  
  95.              httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));   
  96. //          StringEntity entity1 = new StringEntity(getUTF8XMLString(reqXml), "UTF-8");  
  97. //          entity1.setContentType("text/xml;charset=UTF-8");  
  98. //          entity1.setContentEncoding("UTF-8");  
  99. //          httppost.setEntity(entity1);  
  100.             //设置报文头  
  101.             httppost.setHeader("Content-Type", "text/xml;charset=UTF-8");  
  102.             // 发送请求  
  103.             HttpResponse httpresponse = httpClient.execute(httppost);  
  104.             // 获取返回数据  
  105.             HttpEntity entity = httpresponse.getEntity();  
  106.             body = EntityUtils.toString(entity);  
  107.             if (entity != null) {  
  108.                 entity.consumeContent();  
  109.             }  
  110.         } catch (UnsupportedEncodingException e) {  
  111.             e.printStackTrace();  
  112.         } catch (ClientProtocolException e) {  
  113.             e.printStackTrace();  
  114.         } catch (ParseException e) {  
  115.             e.printStackTrace();  
  116.         } catch (IOException e) {  
  117.             e.printStackTrace();  
  118.         }  
  119.         return body;  
  120.     }  
  121.     /**  
  122.      * 根据样式格式化时间  
  123.      * "yyyyMMdd"  
  124.      * "yyyyMMddHHmmss"  
  125.      * "yyyyMMddHHmmssssssss"  
  126.      * @param dateFormat  
  127.      * @return  
  128.      */  
  129.     public static String getnowDate(String dateFormat)  
  130.     {  
  131.         SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);   
  132.         return sdf.format(new Date());  
  133.     }  
  134.   
  135. }

猜你喜欢

转载自aoyouzi.iteye.com/blog/2123607