org.apache.commons.httpclient.HttpClient请求

lot.ticket.api.url = http://api.08cai.local/ipub/open-bet-api.action

 

 

 

 

 

 

 

 

 

private final Logger log = Logger.getLogger(MoNPlam.class);
 private static MoNPlam plam = null;

 // 设置连接超时时间(单位毫秒)
// private int CONNECTION_TIME_OUT = 30000;
 // 设置读数据超时时间(单位毫秒)
 private int SO_TIME_OUT = 30000;

 private String url;
// private String key;
// private String partnerid;
// private String transcode;
// private String header;
 
 private MoNPlam() {
  url = ConfigureManager.getProperties("lot.ticket.api.url");
 }

 public static MoNPlam getInStance() {
  if (plam == null) {
   return new MoNPlam();
  }
  return plam;
 }

 private String init(String partnerid, String transcode) {
  String time = DatetimeUtil.convertDateToStr(new Date(), "yyyyMMddHHmmss");
  return ""
    + "     + partnerid + "\" version=\"2.0\" time=\""+time+"\"/>"
    + "";
 }

 public String sendXml(String transcode, String partnerid, String body,
   String key) {
  if (null == transcode || null == partnerid || null == body
    || null == key) {
   return "传输参数错误";
  }
  

  String header = init(partnerid, transcode);
  return this.sendRequest(header, body, transcode, partnerid, key);
 }

 private String sendRequest(String header, String body, String transcode, String partnerid, String key) {
  String msg = header + body + "";

  HttpClient httpclient = new HttpClient();
  HttpConnectionManagerParams httpParams = httpclient
    .getHttpConnectionManager().getParams();
  // 设置连接超时时间(单位毫秒)
  // httpParams.setConnectionTimeout(CONNECTION_TIME_OUT);
  // 设置读数据超时时间(单位毫秒)
  httpParams.setSoTimeout(SO_TIME_OUT);

  // post请求
  PostMethod postmethod = new PostMethod(url);
  postmethod.addRequestHeader("Connection", "Keep-Alive");
  postmethod.getParams().setContentCharset("utf-8");

  try {
   NameValuePair[] postData = new NameValuePair[5];
   postData[0] = new NameValuePair("partnerid", partnerid);
   postData[1] = new NameValuePair("transcode", transcode);
   postData[2] = new NameValuePair("version", "2.0");
   String pakey = MD5.encrypt(transcode + msg + key).toUpperCase();
   postData[3] = new NameValuePair("key", pakey);
   postData[4] = new NameValuePair("msg", msg);
   postmethod.addParameters(postData);

   String data = "transcode=" + transcode + "&msg=" + msg + "&key="
     + pakey + "&partnerid=" + partnerid;
   log.info("发送数据::" + data);

   int sendStatus = 0;
   sendStatus = httpclient.executeMethod(postmethod);
   if (sendStatus != HttpStatus.SC_OK) {
    // throw new
    // Exception("HttpClient.executeMethod response statusCode not correct!");
   }
   log.info("接收数据:" + postmethod.getResponseBodyAsString());
   return postmethod.getResponseBodyAsString();
  } catch (HttpException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   // 释放
   postmethod.releaseConnection();
  }
  return "系统错误";
 }

 

你可能感兴趣的:(JAVA)