HTTPRequestUtils.java 4.8 KB

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