HttpClientUtil.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. import org.json.JSONObject;
  17. import org.springframework.http.HttpHeaders;
  18. import org.springframework.http.MediaType;
  19. import org.springframework.web.client.RestTemplate;
  20. public class HttpClientUtil {
  21. /**
  22. * 发送post请求
  23. * @param url 请求地址
  24. * @param params 请求参数
  25. * @param chatSet 编码格式
  26. * @return
  27. */
  28. public static String post(String url, List<NameValuePair> params, String chatSet) {
  29. // 创建默认的httpClient实例.
  30. CloseableHttpClient httpclient = HttpClients.createDefault();
  31. // 创建httppost
  32. HttpPost httppost = new HttpPost(url);
  33. UrlEncodedFormEntity uefEntity;
  34. try {
  35. uefEntity = new UrlEncodedFormEntity(params, chatSet);
  36. httppost.setEntity(uefEntity);
  37. CloseableHttpResponse response = httpclient.execute(httppost);
  38. try {
  39. HttpEntity entity = response.getEntity();
  40. if (entity != null) {
  41. return EntityUtils.toString(entity, chatSet);
  42. }
  43. } finally {
  44. response.close();
  45. }
  46. } catch (ClientProtocolException e) {
  47. e.printStackTrace();
  48. } catch (UnsupportedEncodingException e1) {
  49. e1.printStackTrace();
  50. } catch (IOException e) {
  51. e.printStackTrace();
  52. } finally {
  53. // 关闭连接,释放资源
  54. try {
  55. httpclient.close();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. return null;
  61. }
  62. /**
  63. * 发送get请求
  64. * @param url 请求地址
  65. * @param chatSet 编码格式
  66. * @return
  67. */
  68. public static String get(String url, String chatSet) {
  69. CloseableHttpClient httpclient = HttpClients.createDefault();
  70. try {
  71. // 创建httpget.
  72. HttpGet httpget = new HttpGet(url);
  73. // 执行get请求.
  74. CloseableHttpResponse response = httpclient.execute(httpget);
  75. try {
  76. // 获取响应实体
  77. HttpEntity entity = response.getEntity();
  78. if (entity != null) {
  79. return EntityUtils.toString(entity, chatSet);
  80. }
  81. } finally {
  82. response.close();
  83. }
  84. } catch (ClientProtocolException e) {
  85. e.printStackTrace();
  86. } catch (ParseException e) {
  87. e.printStackTrace();
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. } finally {
  91. // 关闭连接,释放资源
  92. try {
  93. httpclient.close();
  94. } catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. }
  98. return null;
  99. }
  100. public static String postBody(String url,JSONObject params){
  101. RestTemplate restTemplate = new RestTemplate();
  102. HttpHeaders headers = new HttpHeaders();
  103. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  104. headers.setContentType(type);
  105. headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  106. org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
  107. String ret = restTemplate.postForObject(url, formEntity, String.class);
  108. return ret;
  109. }
  110. public static void putBody(String url,JSONObject params){
  111. RestTemplate restTemplate = new RestTemplate();
  112. HttpHeaders headers = new HttpHeaders();
  113. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  114. headers.setContentType(type);
  115. headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  116. org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
  117. restTemplate.put(url, formEntity, String.class);
  118. }
  119. }