HttpUtils.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. *
  3. * Copyright (c) behosoft Co.,Ltd.
  4. * All Rights Reserved.
  5. *
  6. * This software is the confidential and proprietary information of behosoft.
  7. * (Social Security Department). You shall not disclose such
  8. * Confidential Information and shall use it only in accordance with
  9. * the terms of the license agreement you entered into with behosoft.
  10. *
  11. * Distributable under GNU LGPL license by gnu.org
  12. */
  13. package com.behosoft.util;
  14. import java.io.BufferedReader;
  15. import java.io.ByteArrayInputStream;
  16. import java.io.InputStream;
  17. import java.io.InputStreamReader;
  18. import java.io.PrintWriter;
  19. import java.net.HttpURLConnection;
  20. import java.net.URL;
  21. import java.net.URLConnection;
  22. import java.net.URLEncoder;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import net.sf.json.JSONObject;
  27. import org.apache.commons.httpclient.HttpClient;
  28. import org.apache.commons.httpclient.methods.PostMethod;
  29. import org.apache.log4j.Logger;
  30. import com.behosoft.framework.web.dto.HttpRequest;
  31. /**
  32. *
  33. * <p>
  34. * Title: 百弘电商物流标准版_[]_[模块名]
  35. * </p>
  36. * <p>
  37. * Description: [描述该类概要功能介绍]
  38. * </p>
  39. * @author jiangyonghua
  40. * @version 1.0 2019-1-31
  41. * @author (lastest modification by )
  42. * @since 1.0
  43. */
  44. public class HttpUtils {
  45. private static Logger logger = Logger.getLogger(HttpUtils.class);
  46. public static Map<String, String> httpPost(Map<String, Object> params){
  47. Map<String, String> result = new HashMap<String, String>();
  48. try
  49. {
  50. String url = params.get("url").toString();
  51. String requestData = params.get("requestData").toString();
  52. logger.info("url: " + url + ",requestData: " + requestData);
  53. HttpClient httpClient = new HttpClient();
  54. PostMethod postMethod = new PostMethod(url);
  55. byte[] postdata = requestData.getBytes("UTF-8");
  56. postMethod.setRequestHeader("Content-Type", "application/json");
  57. ByteArrayInputStream bis = new ByteArrayInputStream(postdata);
  58. postMethod.setRequestBody(bis);
  59. postMethod.setRequestContentLength(postdata.length);
  60. int statusCode = httpClient.executeMethod(postMethod);
  61. String responseStr = postMethod.getResponseBodyAsString();
  62. result.put("response", responseStr);
  63. logger.info("statusCode: " + statusCode + ",responseStr: " + responseStr);
  64. }
  65. catch (Exception e)
  66. {
  67. logger.error("服务器异常:" + e.getMessage());
  68. }
  69. return result;
  70. }
  71. public static Map<String, String> httpGet(Map<String, Object> params){
  72. Map<String, String> result = new HashMap<String, String>();
  73. try
  74. {
  75. String path = params.get("path").toString();
  76. String carrierTrackingNumber = params.get("carrierTrackingNumber").toString();
  77. String status = params.get("status").toString();
  78. logger.info("request: " + path + "?carrierTrackingNumber=" + carrierTrackingNumber + "&status=" + status);
  79. String urlNameString = path + "?carrierTrackingNumber=" + carrierTrackingNumber + "&status=" + URLEncoder.encode(status, "UTF-8");
  80. URL url = new URL(urlNameString);
  81. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  82. conn.setRequestProperty("accept", "*/*");
  83. conn.setRequestProperty("connection", "Keep-Alive");
  84. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
  85. conn.setDoOutput(true);
  86. conn.setDoInput(true);
  87. conn.setRequestMethod("GET");
  88. conn.connect();
  89. //获取URLConnection对象对应的输入流
  90. InputStream is = conn.getInputStream();
  91. //构造一个字符流缓存
  92. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  93. String str = "";
  94. StringBuffer sb = new StringBuffer();
  95. while ((str = br.readLine()) != null) {
  96. str = new String(str.getBytes(),"UTF-8");
  97. sb.append(str);
  98. }
  99. is.close();
  100. conn.disconnect();
  101. result.put("response", sb.toString());
  102. logger.info("response: " + sb.toString());
  103. }
  104. catch (Exception e)
  105. {
  106. logger.error("服务器异常:" + e.getMessage());
  107. }
  108. return result;
  109. }
  110. public static void main(String[] args) {
  111. System.out.println("");
  112. }
  113. }