post请求发送json数据

悠悠 2022-12-23 15:27 359阅读 0赞
  1. package com.herocheer.bms.sys.util;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.apache.http.client.methods.CloseableHttpResponse;
  4. import org.apache.http.client.methods.HttpPost;
  5. import org.apache.http.entity.ContentType;
  6. import org.apache.http.entity.StringEntity;
  7. import org.apache.http.impl.client.CloseableHttpClient;
  8. import org.apache.http.impl.client.HttpClients;
  9. import org.apache.http.util.EntityUtils;
  10. import java.io.BufferedReader;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.io.OutputStreamWriter;
  14. import java.net.HttpURLConnection;
  15. import java.net.URL;
  16. public class HttpRequest {
  17. public static String sendPost(String url,String param){
  18. OutputStreamWriter out =null;
  19. BufferedReader reader = null;
  20. String response = "";
  21. //创建连接
  22. try {
  23. URL httpUrl = null; //HTTP URL类 用这个类来创建连接
  24. //创建URL
  25. httpUrl = new URL(url);
  26. //建立连接
  27. HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
  28. conn.setRequestMethod("POST");
  29. conn.setRequestProperty("Content-Type", "application/json");
  30. conn.setRequestProperty("connection", "keep-alive");
  31. conn.setUseCaches(false);//设置不要缓存
  32. conn.setInstanceFollowRedirects(true);
  33. conn.setConnectTimeout(35000);
  34. conn.setReadTimeout(35000);
  35. conn.setDoOutput(true);
  36. conn.setDoInput(true);
  37. conn.connect();
  38. //POST请求
  39. out = new OutputStreamWriter(
  40. conn.getOutputStream(),"utf-8");//这边不指定编码,正式环境中文数据会请求报错,报400
  41. out.write(param);
  42. out.flush();
  43. //读取响应
  44. reader = new BufferedReader(new InputStreamReader(
  45. conn.getInputStream()));
  46. String lines;
  47. while ((lines = reader.readLine()) != null) {
  48. lines = new String(lines.getBytes(), "utf-8");
  49. response+=lines;
  50. }
  51. reader.close();
  52. // 断开连接
  53. conn.disconnect();
  54. } catch (Exception e) {
  55. System.out.println("发送 POST 请求出现异常!"+e);
  56. e.printStackTrace();
  57. }
  58. //使用finally块来关闭输出流、输入流
  59. finally{
  60. try{
  61. if(out!=null){
  62. out.close();
  63. }
  64. if(reader!=null){
  65. reader.close();
  66. }
  67. }
  68. catch(IOException ex){
  69. ex.printStackTrace();
  70. }
  71. }
  72. return response;
  73. }
  74. public static String sendPost2(String url, String data) {
  75. String response = null;
  76. try {
  77. CloseableHttpClient httpclient = null;
  78. CloseableHttpResponse httpresponse = null;
  79. try {
  80. httpclient = HttpClients.createDefault();
  81. HttpPost httppost = new HttpPost(url);
  82. StringEntity stringentity = new StringEntity(data,
  83. ContentType.create("text/json", "UTF-8"));
  84. httppost.setEntity(stringentity);
  85. httpresponse = httpclient.execute(httppost);
  86. response = EntityUtils
  87. .toString(httpresponse.getEntity());
  88. } finally {
  89. if (httpclient != null) {
  90. httpclient.close();
  91. }
  92. if (httpresponse != null) {
  93. httpresponse.close();
  94. }
  95. }
  96. } catch (Exception e) {
  97. e.printStackTrace();
  98. }
  99. return response;
  100. }
  101. public static void main(String[] args) {
  102. JSONObject jsonParam = new JSONObject();
  103. jsonParam.put("id", "12306");
  104. jsonParam.put("name", "孙悟空");
  105. String param = jsonParam.toJSONString();
  106. String url="http://localhost:8080/demo";
  107. String sendPost = sendPost2(url, param);
  108. System.out.println(sendPost);
  109. }
  110. }

发表评论

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

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

相关阅读