package com.froad.points.bankserver.util;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.net.SocketTimeoutException;
import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.froad.points.bankserver.constant.Constants;
import com.froad.points.bankserver.exception.common.FroadException;
/**
*
* <pre>
* MultiThreadHttpClient 返回字符串和bety[]
* </pre>
*
* @author xueyunlong
* @create 2014年9月15日 下午4:54:24
* @modify
* @since JDK1.6
*/
public class MultiThreadHttpClient {
private static Log logger = LogFactory.getLog(MultiThreadHttpClient.class);
@SuppressWarnings("deprecation")
public static String postMethod(String reqUrl, String strStream) throws FroadException {
String returnStr = "";
logger.info("regUrl:"+reqUrl);
// 多线程
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpClient httpClient = new HttpClient(connectionManager);
// 测试是否超时
HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
// 设置连接超时时间(单位毫秒)
managerParams.setConnectionTimeout(200000);
// 设置读数据超时时间(单位毫秒)
managerParams.setSoTimeout(180000);
PostMethod postMethod = new PostMethod(reqUrl);
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
try {
postMethod.setRequestBody(strStream);
long startTime = System.currentTimeMillis();
int status = httpClient.executeMethod(postMethod);
//System.out.println("postMethod=============="+postMethod);
long endTime = System.currentTimeMillis();
logger.info(status+"status,连接用时" + (endTime - startTime) + "ms");
if(status==HttpStatus.SC_OK){
BufferedReader br=new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(),"utf-8"));
String line = "";
StringBuffer buffer = new StringBuffer();
while ((line = br.readLine()) != null) {
buffer.append(line);
}
logger.info("响应体:"+buffer.toString());
returnStr = buffer.toString();
} else {
logger.info("服务器HTTP响应异常!状态码:"+status);
throw new FroadException(Constants.RESULT_FAIL,"发生Http异常!");
}
} catch (HttpException e) {
logger.info("发生Http异常:" + e);
throw new FroadException(Constants.RESULT_FAIL,"发生Http异常!");
} catch (NoRouteToHostException e) {
logger.info("本机未联网:" + e);
throw new FroadException(Constants.RESULT_FAIL,"本机未联网!");
} catch (ConnectException e) {
logger.info("连接不上服务器:" + e);
throw new FroadException(Constants.RESULT_FAIL,"连接不上服务器!");
} catch (SocketTimeoutException e) {
logger.info("读取数据超时:" + e);
throw new FroadException(Constants.RESULT_PROCESSING,"读取数据超时!");
} catch (ConnectTimeoutException e) {
logger.info("连接超时:" + e);
throw new FroadException(Constants.RESULT_PROCESSING,"连接超时!");
} catch (IOException e) {
logger.info("发生网络异常:" + e);
throw new FroadException(Constants.RESULT_FAIL,"发生网络异常!");
} catch(Exception e) {
logger.info("发生异常:" + e);
throw new FroadException(Constants.RESULT_FAIL,"发生异常!");
} finally {
postMethod.releaseConnection();
}
return returnStr;
}
@SuppressWarnings("deprecation")
public static byte[] postAsStreamMethod(String reqUrl, String strStream) throws FroadException {
logger.info("regUrl:"+reqUrl);
// 多线程
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpClient httpClient = new HttpClient(connectionManager);
// 测试是否超时
HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
// 设置连接超时时间(单位毫秒)
managerParams.setConnectionTimeout(200000);
// 设置读数据超时时间(单位毫秒)
managerParams.setSoTimeout(180000);
PostMethod postMethod = new PostMethod(reqUrl);
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
try {
postMethod.setRequestBody(strStream);
long startTime = System.currentTimeMillis();
int status = httpClient.executeMethod(postMethod);
//System.out.println("postMethod=============="+postMethod);
long endTime = System.currentTimeMillis();
logger.info(status+"status,连接用时" + (endTime - startTime) + "ms");
if(status==HttpStatus.SC_OK){
byte[] b=InputStreamToByte(postMethod.getResponseBodyAsStream());
return b;
} else {
logger.info("服务器HTTP响应异常!状态码:"+status);
throw new FroadException(Constants.RESULT_FAIL,"发生Http异常!");
}
} catch (HttpException e) {
logger.info("发生Http异常:" + e);
throw new FroadException(Constants.RESULT_FAIL,"发生Http异常!");
} catch (NoRouteToHostException e) {
logger.info("本机未联网:" + e);
throw new FroadException(Constants.RESULT_FAIL,"本机未联网!");
} catch (ConnectException e) {
logger.info("连接不上服务器:" + e);
throw new FroadException(Constants.RESULT_FAIL,"连接不上服务器!");
} catch (SocketTimeoutException e) {
logger.info("读取数据超时:" + e);
throw new FroadException(Constants.RESULT_PROCESSING,"读取数据超时!");
} catch (ConnectTimeoutException e) {
logger.info("连接超时:" + e);
throw new FroadException(Constants.RESULT_PROCESSING,"连接超时!");
} catch (IOException e) {
logger.info("发生网络异常:" + e);
throw new FroadException(Constants.RESULT_FAIL,"发生网络异常!");
} catch(Exception e) {
logger.info("发生异常:" + e);
throw new FroadException(Constants.RESULT_FAIL,"发生异常!");
} finally {
postMethod.releaseConnection();
}
}
public static byte[] InputStreamToByte(InputStream is) throws IOException {
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
bytestream.write(ch);
}
byte imgdata[] = bytestream.toByteArray();
bytestream.close();
return imgdata;
}
}