HttpClientService.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.caimei.util.helipay;
  2. import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
  3. import org.apache.commons.httpclient.HttpClient;
  4. import org.apache.commons.httpclient.HttpStatus;
  5. import org.apache.commons.httpclient.NameValuePair;
  6. import org.apache.commons.httpclient.methods.PostMethod;
  7. import org.apache.commons.httpclient.params.HttpMethodParams;
  8. import org.apache.commons.logging.Log;
  9. import org.apache.commons.logging.LogFactory;
  10. import java.io.BufferedReader;
  11. import java.io.DataOutputStream;
  12. import java.io.IOException;
  13. import java.io.InputStreamReader;
  14. import java.net.HttpURLConnection;
  15. import java.net.URL;
  16. import java.util.*;
  17. public class HttpClientService {
  18. private static final Log LOG = LogFactory.getLog(HttpClientService.class);
  19. public static Map<String, Object> getHttpResp(Map<String, String> reqMap, String httpUrl) {
  20. HttpClient client = new HttpClient();
  21. PostMethod method = new PostMethod(httpUrl);
  22. method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
  23. method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
  24. method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(300000));
  25. String response = "";
  26. Map<String, Object> mp = new HashMap<String, Object>();
  27. try {
  28. NameValuePair[] nvps = getNameValuePair(reqMap);
  29. method.setRequestBody(nvps);
  30. int rescode = client.executeMethod(method);
  31. mp.put("statusCode", rescode);
  32. LOG.info("http rescode:" + rescode);
  33. if (rescode == HttpStatus.SC_OK) {
  34. BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
  35. String curline = "";
  36. while ((curline = reader.readLine()) != null) {
  37. response += curline;
  38. }
  39. LOG.info("http response:" + response);
  40. mp.put("response", response);
  41. }
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. } finally {
  45. method.releaseConnection();
  46. }
  47. return mp;
  48. }
  49. /**
  50. *
  51. * HTTP协议POST请求方法
  52. */
  53. public static String httpMethodPost(String url, String params, String gb) {
  54. if (null == gb || "".equals(gb)) {
  55. gb = "UTF-8";
  56. }
  57. StringBuffer sb = new StringBuffer();
  58. URL urls;
  59. HttpURLConnection uc = null;
  60. BufferedReader in = null;
  61. DataOutputStream out = null;
  62. try {
  63. urls = new URL(url);
  64. uc = (HttpURLConnection) urls.openConnection();
  65. uc.setRequestMethod("POST");
  66. uc.setDoOutput(true);
  67. uc.setDoInput(true);
  68. uc.setUseCaches(false);
  69. uc.setRequestProperty("Connection", "keep-alive");
  70. uc.setRequestProperty("Keep-Alive", "timeout=1, max=100");
  71. uc.setRequestProperty("Content-Length", params.length() + "");
  72. uc.setRequestProperty("Content-Type","application/json");
  73. uc.setConnectTimeout(7000);
  74. uc.setReadTimeout(10000);
  75. uc.connect();
  76. out = new DataOutputStream(uc.getOutputStream());
  77. out.write(params.getBytes(gb));
  78. out.flush();
  79. out.close();
  80. in = new BufferedReader(new InputStreamReader(uc.getInputStream(),
  81. gb));
  82. String readLine = "";
  83. while ((readLine = in.readLine()) != null) {
  84. sb.append(readLine);
  85. }
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. } finally {
  89. try {
  90. if (out != null){
  91. out.close();
  92. }
  93. if (in != null){
  94. in.close();
  95. }
  96. if (uc != null) {
  97. uc.disconnect();
  98. }
  99. } catch (IOException e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. return sb.toString();
  104. }
  105. public static NameValuePair[] getNameValuePair(Map<String, String> bean) {
  106. List<NameValuePair> x = new ArrayList<NameValuePair>();
  107. for (Iterator<String> iterator = bean.keySet().iterator(); iterator.hasNext(); ) {
  108. String type = (String) iterator.next();
  109. x.add(new NameValuePair(type, String.valueOf(bean.get(type))));
  110. }
  111. Object[] y = x.toArray();
  112. NameValuePair[] n = new NameValuePair[y.length];
  113. System.arraycopy(y, 0, n, 0, y.length);
  114. return n;
  115. }
  116. }