HttpClient发送post,get请求

淡淡的烟草味﹌ 2023-08-17 16:41 337阅读 0赞

maven依赖:

  1. <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
  2. <dependency>
  3. <groupId>org.apache.httpcomponents</groupId>
  4. <artifactId>httpcore</artifactId>
  5. <version>4.4.10</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
  8. <dependency>
  9. <groupId>org.apache.httpcomponents</groupId>
  10. <artifactId>httpclient</artifactId>
  11. <version>4.5.6</version>
  12. </dependency>

工具类:

  1. package cn.com.dzqc.util;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.List;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.client.entity.UrlEncodedFormEntity;
  9. import org.apache.http.client.methods.CloseableHttpResponse;
  10. import org.apache.http.client.methods.HttpGet;
  11. import org.apache.http.client.methods.HttpPost;
  12. import org.apache.http.entity.StringEntity;
  13. import org.apache.http.impl.client.CloseableHttpClient;
  14. import org.apache.http.impl.client.HttpClients;
  15. import org.apache.http.message.BasicHeader;
  16. import org.apache.http.util.EntityUtils;
  17. import org.json.JSONObject;
  18. /**
  19. * HttpClient工具类
  20. *
  21. * @author 煕寳
  22. *
  23. */
  24. public class HttpClientUtil {
  25. public static String sendGet(String s) {
  26. CloseableHttpClient httpClient = HttpClients.createDefault();
  27. StringBuilder entityStringBuilder = null;
  28. try {
  29. HttpGet get = new HttpGet(s);
  30. CloseableHttpResponse httpResponse = null;
  31. httpResponse = httpClient.execute(get);
  32. try {
  33. HttpEntity entity = httpResponse.getEntity();
  34. entityStringBuilder = new StringBuilder();
  35. if (null != entity) {
  36. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"), 8 * 1024);
  37. String line = null;
  38. while ((line = bufferedReader.readLine()) != null) {
  39. entityStringBuilder.append(line + "/n");
  40. }
  41. }
  42. } finally {
  43. httpResponse.close();
  44. }
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. } finally {
  48. try {
  49. if (httpClient != null) {
  50. httpClient.close();
  51. }
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. return entityStringBuilder.toString();
  57. }
  58. /**
  59. * 发送POST请求
  60. * @param url
  61. * @param nameValuePairList
  62. * @return JSON或者字符串
  63. * @throws Exception
  64. */
  65. public static String sendPost(String url, List<NameValuePair> nameValuePairList) throws Exception{
  66. JSONObject jsonObject = null;
  67. CloseableHttpClient client = null;
  68. CloseableHttpResponse response = null;
  69. String result ="";
  70. try{
  71. /**
  72. * 创建一个httpclient对象,相当于打开一个浏览器
  73. */
  74. client = HttpClients.createDefault();
  75. /**
  76. * 创建一个post对象
  77. */
  78. HttpPost post = new HttpPost(url);
  79. /**
  80. * 包装成一个Entity对象
  81. */
  82. StringEntity entity = new UrlEncodedFormEntity(nameValuePairList, "UTF-8");
  83. /**
  84. * 设置请求的内容
  85. */
  86. post.setEntity(entity);
  87. /**
  88. * 设置请求的报文头部的编码
  89. */
  90. post.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"));
  91. /**
  92. * 设置请求的报文头部的编码
  93. */
  94. post.setHeader(new BasicHeader("Accept", "text/plain;charset=utf-8"));
  95. /**
  96. * 执行post请求
  97. */
  98. response = client.execute(post);
  99. /**
  100. * 通过EntityUitls获取返回内容
  101. */
  102. result = EntityUtils.toString(response.getEntity(),"UTF-8");
  103. }catch (Exception e){
  104. e.printStackTrace();
  105. }finally {
  106. response.close();
  107. client.close();
  108. }
  109. return result;
  110. }
  111. }

发表评论

表情:
评论列表 (有 0 条评论,337人围观)

还没有评论,来说两句吧...

相关阅读