HTTPRequestUtils.java 6.4 KB

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