访问平台Servlet时,如何设置超时时间

需要的jar包:commons-httpclient-3.1.jar,commons-logging-1.0.4.jar,commons-codec-1.2.jar。在大地财险测试通过。

代码如下所示:

  1. package cn.com.sinosoft.test.http;  
  2.   
  3. import org.apache.commons.httpclient.HostConfiguration;  
  4. import org.apache.commons.httpclient.HttpClient;  
  5. import org.apache.commons.httpclient.HttpStatus;  
  6. import org.apache.commons.httpclient.methods.PostMethod;  
  7. import org.apache.commons.httpclient.methods.StringRequestEntity;  
  8. import org.apache.commons.httpclient.params.HttpConnectionManagerParams;  
  9.   
  10. public class PostSample {  
  11.  public static void main(String[] args) {  
  12.   HttpClient httpClient = new HttpClient();  
  13.   // 设置代理  
  14.   // HostConfiguration hcf = new HostConfiguration();  
  15.   // hcf.setProxy("localhost", 8118);  
  16.   // httpClient.setHostConfiguration(hcf);  
  17.   
  18.   HttpConnectionManagerParams managerParams = httpClient  
  19.     .getHttpConnectionManager().getParams();  
  20.   // 设置连接超时时间(单位毫秒)  
  21.   managerParams.setConnectionTimeout(30000);  
  22.   // 设置读数据超时时间(单位毫秒)  
  23.   managerParams.setSoTimeout(120000);  
  24.   
  25.   String url = "http://localhost/testweb/commserver";  
  26.   PostMethod postMethod = new PostMethod(url);  
  27.   
  28.   // 将请求参数XML的值放入postMethod中  
  29.   String strResponse = null;  
  30.   try {  
  31.    postMethod.setRequestEntity(new StringRequestEntity(  
  32.      createRequestXML(), "text/xml""GBK"));  
  33.    int statusCode = httpClient.executeMethod(postMethod);  
  34.    if (statusCode != HttpStatus.SC_OK) {  
  35.     throw new IllegalStateException("Method failed: "  
  36.       + postMethod.getStatusLine());  
  37.    }  
  38.    strResponse = postMethod.getResponseBodyAsString();  
  39.   } catch (Exception ex) {  
  40.    throw new IllegalStateException(ex.toString());  
  41.   } finally {  
  42.    // 释放连接  
  43.    postMethod.releaseConnection();  
  44.   }  
  45.   System.out.println(strResponse);  
  46.   
  47.  }  
  48.   
  49.  public static String createRequestXML() {  
  50.   StringBuffer buffer = new StringBuffer();  
  51.   buffer.append("<?xml version=/"1.0/" encoding=/"GBK/"?>");  
  52.   buffer.append("<PACKET>");  
  53.   buffer.append("<HEAD>");  
  54.   buffer.append("<REQUEST_TYPE>01</REQUEST_TYPE>");  
  55.   buffer.append("</HEAD>");  
  56.   buffer.append("</PACKET>");  
  57.   return buffer.toString();  
  58.   
  59.  }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值