| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package xin.glue.cargocnHttpClient;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.PrintStream;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.http.Header;
- import org.apache.http.HttpRequest;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.params.HttpConnectionParams;
- import org.apache.http.util.EntityUtils;
- public class HttpPostUtil {
- // 接口地址
- private String apiURL = "";
- private Log logger = LogFactory.getLog(this.getClass());
- private static final String pattern = "yyyy-MM-dd HH:mm:ss:SSS";
- private HttpClient httpClient = null;
- private HttpPost method = null;
- private int status = 0;
- /**
- * 接口地址
- *
- * @param url
- */
- public HttpPostUtil(String url) {
- if (url != null) {
- this.apiURL = url;
- }
- if (apiURL != null) {
- httpClient = new DefaultHttpClient();
- method = new HttpPost(apiURL);
- }
- }
- /**
- * 调用 API
- *
- * @param parameters
- * @return
- */
- public String post(String bill, String sign) {
- String body = null;
- try {
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- // 建立一个NameValuePair数组,用于存储欲传送的参数
- params.add(new BasicNameValuePair("bill", bill));
- params.add(new BasicNameValuePair("sign", sign));
- // 添加参数
- method.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
- /*连接超时8秒*/
- HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 8000);
- /*请求超时*/
- // HttpConnectionParams.setSoTimeout(httpClient.getParams(), 8000);
- // 设置编码
- HttpResponse response = httpClient.execute(method);
- int statusCode = response.getStatusLine().getStatusCode();
- logger.info("statusCode:" + statusCode);
- Header header = response.getFirstHeader("retStatus");
- String strStatu = header.getValue();
- if ("-1".equals(strStatu)) {
- // logger.error("Method failed:" + response.getStatusLine());
- status = 1;
- }
- // Read the response body
- body = strStatu;
- } catch (IOException e) {
- logger.error("exception occurred!\n", e);
- // 网络错误
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- e.printStackTrace(new PrintStream(baos));
- String exception = baos.toString();
- body = "\n网络异常原因:" + exception;
- e.printStackTrace();
- status = 3;
- } finally {
- logger.info("调用接口状态:" + status);
- }
- return body;
- }
- /**
- * 0.成功 1.执行方法失败 2.协议错误 3.网络错误
- *
- * @return the status
- */
- public int getStatus() {
- return status;
- }
- /**
- * @param status
- * the status to set
- */
- public void setStatus(int status) {
- this.status = status;
- }
- }
|