HttpClientUtil.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.yihu.wlyy.util;
  2. import java.io.IOException;
  3. import java.io.UnsupportedEncodingException;
  4. import java.util.List;
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.NameValuePair;
  7. import org.apache.http.ParseException;
  8. import org.apache.http.client.ClientProtocolException;
  9. import org.apache.http.client.entity.UrlEncodedFormEntity;
  10. import org.apache.http.client.methods.CloseableHttpResponse;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.client.methods.HttpPost;
  13. import org.apache.http.impl.client.CloseableHttpClient;
  14. import org.apache.http.impl.client.HttpClients;
  15. import org.apache.http.util.EntityUtils;
  16. public class HttpClientUtil {
  17. /**
  18. * 发送post请求
  19. * @param url 请求地址
  20. * @param params 请求参数
  21. * @param chatSet 编码格式
  22. * @return
  23. */
  24. public static String post(String url, List<NameValuePair> params, String chatSet) {
  25. // 创建默认的httpClient实例.
  26. CloseableHttpClient httpclient = HttpClients.createDefault();
  27. // 创建httppost
  28. HttpPost httppost = new HttpPost(url);
  29. UrlEncodedFormEntity uefEntity;
  30. try {
  31. uefEntity = new UrlEncodedFormEntity(params, chatSet);
  32. httppost.setEntity(uefEntity);
  33. CloseableHttpResponse response = httpclient.execute(httppost);
  34. try {
  35. HttpEntity entity = response.getEntity();
  36. if (entity != null) {
  37. return EntityUtils.toString(entity, chatSet);
  38. }
  39. } finally {
  40. response.close();
  41. }
  42. } catch (ClientProtocolException e) {
  43. e.printStackTrace();
  44. } catch (UnsupportedEncodingException e1) {
  45. e1.printStackTrace();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. } finally {
  49. // 关闭连接,释放资源
  50. try {
  51. httpclient.close();
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. return null;
  57. }
  58. /**
  59. * 发送get请求
  60. * @param url 请求地址
  61. * @param chatSet 编码格式
  62. * @return
  63. */
  64. public static String get(String url, String chatSet) {
  65. CloseableHttpClient httpclient = HttpClients.createDefault();
  66. try {
  67. // 创建httpget.
  68. HttpGet httpget = new HttpGet(url);
  69. // 执行get请求.
  70. CloseableHttpResponse response = httpclient.execute(httpget);
  71. try {
  72. // 获取响应实体
  73. HttpEntity entity = response.getEntity();
  74. if (entity != null) {
  75. return EntityUtils.toString(entity, chatSet);
  76. }
  77. } finally {
  78. response.close();
  79. }
  80. } catch (ClientProtocolException e) {
  81. e.printStackTrace();
  82. } catch (ParseException e) {
  83. e.printStackTrace();
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. } finally {
  87. // 关闭连接,释放资源
  88. try {
  89. httpclient.close();
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. }
  93. }
  94. return null;
  95. }
  96. }