用httpclient4.x 发送http get post请求。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值