ad66a5410f685d972d31e405e35a90e8ba740b33.svn-base 3.1 KB

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