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);
}
}