org.apache.commons.httpclient.HttpClient请求

本文介绍了一个彩票系统的API交互过程,包括初始化请求头、发送XML格式的消息体及接收响应的方法。通过HttpClient实现POST请求,详细展示了如何构造请求参数、加密验证等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
    + "<msg><head transcode=\"" + transcode + "\" partnerid=\""
    + partnerid + "\" version=\"2.0\" time=\""+time+"\"/>"
    + "<body>";
 }

 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 + "</body></msg>";

  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 "系统错误";
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值