HttpClient 实现类

public class AppHttpClient extends HttpClient{
 
 protected static Logger logger = Logger.getRootLogger();
 
 /***
  * DEFAULT 链接时间:30s;
  * DEFAULT 等待响应时间:5s;
  */
 public AppHttpClient(){
  super();
  
  super.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
  super.getHttpConnectionManager().getParams().setSoTimeout(10000);
  
  DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(1, false);
  super.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryhandler);
  
 }
 
 
 public AppHttpClient(int connectionTimeout, int soTimeout, int retryTimes)
 {
  super();
  
  if(connectionTimeout > 0 && connectionTimeout < Integer.MAX_VALUE){
   super.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
  }
  
  if(soTimeout > 0 && soTimeout < Integer.MAX_VALUE){
   super.getHttpConnectionManager().getParams().setSoTimeout(soTimeout);
  }
  
  if(retryTimes > 0 && retryTimes <= 10){
   DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(retryTimes, false);
   super.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryhandler);
  }
 
 }
 
 
 public void spExecuteMethod(HttpMethod method, String spName, String enc)
 {
  String res = this.appExecuteMethod(method, enc);
  if("HttpException".equalsIgnoreCase(res)){
   logger.warn(spName+" mt HttpException");
  }else if("IOException".equals(res)){
   logger.warn(spName+" mt SocketTimeout:Read timed out");
  }else{
   logger.info("请求"+spName+"返回记录:" + res);
  }
  
 }
 
 /***
  * 返回正常字符串、'HttpException'、'IOException'
  */
 public String appExecuteMethod(HttpMethod method, String enc)
 {
  String src = null;
  try {
   int rt = this.executeMethod(method);
    InputStream ins = null;
       ins = method.getResponseBodyAsStream();
       if( ins != null){
        BufferedReader br = new BufferedReader(new InputStreamReader(ins, enc));
     StringBuffer sbf = new StringBuffer();
     String line = null;
    while ((line = br.readLine()) != null) {   
     sbf.append(line);
    }
    src = sbf.toString();
       }
   
  } catch (HttpException e) {
   return "HttpException";
  } catch (IOException e) {
   return "IOException";
  }catch(Exception e){
   e.printStackTrace();
  }finally{
   method.releaseConnection();
  }
  
  return src;
 }
 
}

 

 

发送线程:

 

public class BaoyingThread implements Runnable{

 protected static Logger logger = Logger.getRootLogger();
 private NNWCmd nnwCmd = null;
 private BaoyingResp resp = null;
 
 
 
 public BaoyingThread(NNWCmd nnwCmd, BaoyingResp resp) {
  this.nnwCmd = nnwCmd;
  this.resp = resp;
 }

 

 @Override
 public void run() {
  
  String enc = "UTF-8";
  String mtUrl = this.nnwCmd.getInstruChannel().getMtUrl();
  if(mtUrl == null || "".equals(mtUrl)){
   mtUrl = "http://218.206.29.15:8086/index.aspx?";
  }
  
  if(mtUrl.lastIndexOf("?") <= 0){
   mtUrl = mtUrl+"?";
  }
  
  String msg = resp.getMsg();
  try {
   msg =  URLEncoder.encode(msg,enc);
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  
  StringBuilder builder = new StringBuilder(mtUrl);
  builder.append("Userid="+resp.getUserid())
    .append("&pwd="+resp.getPwd())
    .append("&cid="+resp.getCid())
    .append("&did="+resp.getDid())
          .append("&phone_fee="+resp.getPhone_fee())
          .append("&msg="+msg)
          .append("&feevalue="+resp.getFeevalue())
          .append("&code="+resp.getCode())
          .append("&linkid="+resp.getLinkid())
    .append("&fee_type="+resp.getFee_type());
  String mt = builder.toString();
  logger.info("baoying 下行记录:"+mt);
  GetMethod method = new GetMethod(mt);
  
  AppHttpClient appClient = new AppHttpClient();
  appClient.spExecuteMethod(method, "baoying", enc);
  
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值