HTTPRequestUtils.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.steerinfo.dil.util;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.apache.http.HttpEntity;
  4. import org.apache.http.ParseException;
  5. import org.apache.http.client.methods.CloseableHttpResponse;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.entity.StringEntity;
  8. import org.apache.http.impl.client.CloseableHttpClient;
  9. import org.apache.http.impl.client.HttpClients;
  10. import org.apache.http.message.BasicHeader;
  11. import org.apache.http.protocol.HTTP;
  12. import org.apache.http.util.EntityUtils;
  13. import org.springframework.util.MultiValueMap;
  14. import java.io.*;
  15. import java.net.HttpURLConnection;
  16. import java.net.URL;
  17. import java.util.Map;
  18. public class HTTPRequestUtils {
  19. /**
  20. * 发送post请求
  21. * @param url 路径
  22. * @param json 参数(json类型)
  23. * @param encoding 编码格式
  24. * @return
  25. * @throws ParseException
  26. * @throws IOException
  27. */
  28. public static String send(String url, JSONObject json, String encoding) throws ParseException, IOException {
  29. String body = "";
  30. //创建httpclient对象
  31. CloseableHttpClient client = HttpClients.createDefault();
  32. //创建post方式请求对象
  33. HttpPost httpPost = new HttpPost(url);
  34. //装填参数
  35. StringEntity s = new StringEntity(json.toString(), "utf-8");
  36. s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
  37. //设置参数到请求对象中
  38. httpPost.setEntity(s);
  39. System.out.println("请求地址:" + url);
  40. System.out.println(json);
  41. // System.out.println("请求参数:"+nvps.toString());
  42. //设置header信息
  43. //指定报文头【Content-type】、【User-Agent】
  44. // httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
  45. httpPost.setHeader("Content-type", "application/json");
  46. httpPost.setHeader("-UserAgent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  47. //执行请求操作,并拿到结果(同步阻塞)
  48. CloseableHttpResponse response = client.execute(httpPost);
  49. System.out.println(response);
  50. //获取结果实体
  51. HttpEntity entity = response.getEntity();
  52. System.out.println(entity);
  53. if (entity != null) {
  54. //按指定编码转换结果实体为String类型
  55. body = EntityUtils.toString(entity, encoding);
  56. }
  57. EntityUtils.consume(entity);
  58. //释放链接
  59. response.close();
  60. return body;
  61. }
  62. public static String sendFormData(String url, JSONObject json, String encoding,String hearder) throws ParseException, IOException {
  63. String body = "";
  64. //创建httpclient对象
  65. CloseableHttpClient client = HttpClients.createDefault();
  66. //创建post方式请求对象
  67. HttpPost httpPost = new HttpPost(url);
  68. //装填参数
  69. StringEntity s = new StringEntity(json.toString(), "utf-8");
  70. s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
  71. //设置参数到请求对象中
  72. httpPost.setEntity(s);
  73. System.out.println("请求地址:" + url);
  74. System.out.println(json);
  75. // System.out.println("请求参数:"+nvps.toString());
  76. //设置header信息
  77. //指定报文头【Content-type】、【User-Agent】
  78. // httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
  79. httpPost.setHeader("Content-type", "application/json");
  80. httpPost.setHeader("X-Access-Token",hearder);
  81. httpPost.setHeader("-UserAgent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  82. //执行请求操作,并拿到结果(同步阻塞)
  83. CloseableHttpResponse response = client.execute(httpPost);
  84. System.out.println(response);
  85. //获取结果实体
  86. HttpEntity entity = response.getEntity();
  87. System.out.println(entity);
  88. if (entity != null) {
  89. //按指定编码转换结果实体为String类型
  90. body = EntityUtils.toString(entity, encoding);
  91. }
  92. EntityUtils.consume(entity);
  93. //释放链接
  94. response.close();
  95. return body;
  96. }
  97. public static String getJsonData(JSONObject jsonParam,String urls) {
  98. StringBuffer sb=new StringBuffer();
  99. try {
  100. // 创建url资源
  101. URL url = new URL(urls);
  102. // 建立http连接
  103. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  104. // 设置允许输出
  105. conn.setDoOutput(true);
  106. // 设置允许输入
  107. conn.setDoInput(true);
  108. // 设置不用缓存
  109. conn.setUseCaches(false);
  110. // 设置传递方式
  111. conn.setRequestMethod("POST");
  112. // 设置维持长连接
  113. conn.setRequestProperty("Connection", "Keep-Alive");
  114. // 设置文件字符集:
  115. conn.setRequestProperty("Charset", "UTF-8");
  116. // 转换为字节数组
  117. byte[] data = (jsonParam.toString()).getBytes();
  118. // 设置文件长度
  119. conn.setRequestProperty("Content-Length", String.valueOf(data.length));
  120. // 设置文件类型:
  121. conn.setRequestProperty("contentType", "application/json");
  122. // 开始连接请求
  123. conn.connect();
  124. OutputStream out = new DataOutputStream(conn.getOutputStream()) ;
  125. // 写入请求的字符串
  126. out.write((jsonParam.toString()).getBytes());
  127. out.flush();
  128. out.close();
  129. System.out.println(conn.getResponseCode());
  130. // 请求返回的状态
  131. if (HttpURLConnection.HTTP_OK == conn.getResponseCode()){
  132. System.out.println("连接成功");
  133. // 请求返回的数据
  134. InputStream in1 = conn.getInputStream();
  135. try {
  136. String readLine = new String();
  137. BufferedReader responseReader=new BufferedReader(new InputStreamReader(in1,"UTF-8"));
  138. while((readLine=responseReader.readLine())!=null){
  139. sb.append(readLine).append("\n");
  140. }
  141. responseReader.close();
  142. System.out.println(sb.toString());
  143. } catch (Exception e1) {
  144. e1.printStackTrace();
  145. }
  146. } else {
  147. System.out.println("error++");
  148. }
  149. } catch (Exception e) {
  150. }
  151. return sb.toString();
  152. }
  153. }