25ffedfffbaa7995d1437ecd5d69c1363650e71d.svn-base 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package xin.glue.cargocnHttpClient;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.PrintStream;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import javax.servlet.http.HttpServletRequest;
  8. import org.apache.commons.logging.Log;
  9. import org.apache.commons.logging.LogFactory;
  10. import org.apache.http.Header;
  11. import org.apache.http.HttpRequest;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.HttpStatus;
  14. import org.apache.http.NameValuePair;
  15. import org.apache.http.client.HttpClient;
  16. import org.apache.http.client.entity.UrlEncodedFormEntity;
  17. import org.apache.http.client.methods.HttpPost;
  18. import org.apache.http.impl.client.DefaultHttpClient;
  19. import org.apache.http.message.BasicNameValuePair;
  20. import org.apache.http.params.HttpConnectionParams;
  21. import org.apache.http.util.EntityUtils;
  22. public class HttpPostUtil {
  23. // 接口地址
  24. private String apiURL = "";
  25. private Log logger = LogFactory.getLog(this.getClass());
  26. private static final String pattern = "yyyy-MM-dd HH:mm:ss:SSS";
  27. private HttpClient httpClient = null;
  28. private HttpPost method = null;
  29. private int status = 0;
  30. /**
  31. * 接口地址
  32. *
  33. * @param url
  34. */
  35. public HttpPostUtil(String url) {
  36. if (url != null) {
  37. this.apiURL = url;
  38. }
  39. if (apiURL != null) {
  40. httpClient = new DefaultHttpClient();
  41. method = new HttpPost(apiURL);
  42. }
  43. }
  44. /**
  45. * 调用 API
  46. *
  47. * @param parameters
  48. * @return
  49. */
  50. public String post(String bill, String sign) {
  51. String body = null;
  52. try {
  53. List<NameValuePair> params = new ArrayList<NameValuePair>();
  54. // 建立一个NameValuePair数组,用于存储欲传送的参数
  55. params.add(new BasicNameValuePair("bill", bill));
  56. params.add(new BasicNameValuePair("sign", sign));
  57. // 添加参数
  58. method.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
  59. /*连接超时8秒*/
  60. HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 8000);
  61. /*请求超时*/
  62. // HttpConnectionParams.setSoTimeout(httpClient.getParams(), 8000);
  63. // 设置编码
  64. HttpResponse response = httpClient.execute(method);
  65. int statusCode = response.getStatusLine().getStatusCode();
  66. logger.info("statusCode:" + statusCode);
  67. Header header = response.getFirstHeader("retStatus");
  68. String strStatu = header.getValue();
  69. if ("-1".equals(strStatu)) {
  70. // logger.error("Method failed:" + response.getStatusLine());
  71. status = 1;
  72. }
  73. // Read the response body
  74. body = strStatu;
  75. } catch (IOException e) {
  76. logger.error("exception occurred!\n", e);
  77. // 网络错误
  78. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  79. e.printStackTrace(new PrintStream(baos));
  80. String exception = baos.toString();
  81. body = "\n网络异常原因:" + exception;
  82. e.printStackTrace();
  83. status = 3;
  84. } finally {
  85. logger.info("调用接口状态:" + status);
  86. }
  87. return body;
  88. }
  89. /**
  90. * 0.成功 1.执行方法失败 2.协议错误 3.网络错误
  91. *
  92. * @return the status
  93. */
  94. public int getStatus() {
  95. return status;
  96. }
  97. /**
  98. * @param status
  99. * the status to set
  100. */
  101. public void setStatus(int status) {
  102. this.status = status;
  103. }
  104. }