http工具类
package com.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import com.chinainsurance.application.common.BeanManager;
import com.chinainsurance.application.common.https.MySecureProtocolSocketFactory;
import com.chinainsurance.application.platform.dto.domain.GgRiskConfigValueDto;
import com.chinainsurance.application.platform.persistence.dao.GgRiskConfigValueDao;
public class HttpUtils {
public static String getResponseXml(String requestXml, String url) throws Exception {
StringBuilder buffer = new StringBuilder();
InputStream inputStream = null;
BufferedReader reader = null;
String strMessage = "";
HttpClient httpClient = new HttpClient();
HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
managerParams.setConnectionTimeout(10000);
managerParams.setSoTimeout(60000);
PostMethod postMethod = new PostMethod(url);
try {
postMethod.setRequestEntity(new StringRequestEntity(requestXml,"application/xml", "UTF-8"));
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != 200) {
throw new IllegalStateException("Method failed: " + postMethod.getStatusLine());
}
inputStream = postMethod.getResponseBodyAsStream();
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
while ((strMessage = reader.readLine()) != null){
buffer.append(strMessage);
}
} catch (Exception ex) {
throw new IllegalStateException(ex.toString());
} finally {
postMethod.releaseConnection();
}
return buffer.toString();
}
public static String getResponseJson(String requestJson, String url) throws Exception {
StringBuilder buffer = new StringBuilder();
InputStream inputStream = null;
BufferedReader reader = null;
String strMessage = "";
HttpClient httpClient = new HttpClient();
HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
managerParams.setConnectionTimeout(10000);
managerParams.setSoTimeout(60000);
PostMethod postMethod = new PostMethod(url);
try {
postMethod.setRequestEntity(new StringRequestEntity(requestJson,"application/json", "UTF-8"));
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != 200) {
throw new IllegalStateException("Method failed: " + postMethod.getStatusLine());
}
inputStream = postMethod.getResponseBodyAsStream();
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
while ((strMessage = reader.readLine()) != null){
buffer.append(strMessage);
}
} catch (Exception ex) {
throw new IllegalStateException(ex.toString());
} finally {
postMethod.releaseConnection();
}
return buffer.toString();
}
public static String readRequest(HttpServletRequest request, HttpServletResponse response)throws Exception{
InputStream is = null;
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try{
is = request.getInputStream();
br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
String buffer = null;
while ((buffer = br.readLine()) != null) {
sb.append(buffer+"\n");
}
}catch(Exception e){
throw new RuntimeException("读取请求报文异常");
}finally{
br.close();
is.close();
}
return sb.toString();
}
public static String httpsXml(String requestXml,String url)throws Exception{
StringBuilder buffer = new StringBuilder();
InputStream inputStream = null;
BufferedReader reader = null;
String strMessage = "";
ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();
Protocol.registerProtocol("https", new Protocol("https", fcty, 443));
HttpClient httpClient = new HttpClient();
GgRiskConfigValueDao ggRiskConfigValueDao = (GgRiskConfigValueDao)BeanManager.getBean("ggRiskConfigValueDao");
GgRiskConfigValueDto proxyHostDto = ggRiskConfigValueDao.findByPrimaryKey("03", "1039", "SHUIDI_PROXYHOST");
GgRiskConfigValueDto proxyPortDto = ggRiskConfigValueDao.findByPrimaryKey("03", "1039", "SHUIDI_PROXYPORT");
String proxyHost = "";
Integer proxyPort = 0;
if(proxyHostDto != null && proxyPortDto != null){
proxyHost = proxyHostDto.getConfigValue();
String proxyPortTmp = proxyPortDto.getConfigValue();
if(proxyPortTmp != null && proxyPortTmp.trim().length() >0){
proxyPort = Integer.valueOf(proxyPortTmp);
}
}
httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
httpClient.getParams().setAuthenticationPreemptive(true);
HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
managerParams.setConnectionTimeout(10000);
managerParams.setSoTimeout(60000);
PostMethod postMethod = new PostMethod(url);
try {
postMethod.setRequestEntity(new StringRequestEntity(requestXml,"application/xml", "UTF-8"));
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != 200) {
throw new IllegalStateException("Method failed: " + postMethod.getStatusLine());
}
inputStream = postMethod.getResponseBodyAsStream();
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
while ((strMessage = reader.readLine()) != null){
buffer.append(strMessage);
}
} catch (Exception ex) {
throw new IllegalStateException(ex.toString());
} finally {
postMethod.releaseConnection();
}
return buffer.toString();
}
public static String httpsJson(String requestJson, String url, String proxyInd) throws Exception {
StringBuilder buffer = new StringBuilder();
InputStream inputStream = null;
BufferedReader reader = null;
String strMessage = "";
ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();
Protocol.registerProtocol("https", new Protocol("https", fcty, 443));
HttpClient httpClient = new HttpClient();
GgRiskConfigValueDao ggRiskConfigValueDao = (GgRiskConfigValueDao)BeanManager.getBean("ggRiskConfigValueDao");
GgRiskConfigValueDto proxyHostDto = ggRiskConfigValueDao.findByPrimaryKey("03", "1039", "SHUIDI_PROXYHOST");
GgRiskConfigValueDto proxyPortDto = ggRiskConfigValueDao.findByPrimaryKey("03", "1039", "SHUIDI_PROXYPORT");
String proxyHost = "";
Integer proxyPort = 0;
if(proxyHostDto != null && proxyPortDto != null){
proxyHost = proxyHostDto.getConfigValue();
String proxyPortTmp = proxyPortDto.getConfigValue();
if(proxyPortTmp != null && proxyPortTmp.trim().length() >0){
proxyPort = Integer.valueOf(proxyPortTmp);
}
}
if("1".equals(proxyInd)){
httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
}
httpClient.getParams().setAuthenticationPreemptive(true);
HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
managerParams.setConnectionTimeout(10000);
managerParams.setSoTimeout(60000);
PostMethod postMethod = new PostMethod(url);
try {
postMethod.setRequestEntity(new StringRequestEntity(requestJson,"application/json", "UTF-8"));
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != 200) {
throw new IllegalStateException("Method failed: " + postMethod.getStatusLine());
}
inputStream = postMethod.getResponseBodyAsStream();
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
while ((strMessage = reader.readLine()) != null){
buffer.append(strMessage);
}
} catch (Exception ex) {
throw new IllegalStateException(ex.toString());
} finally {
postMethod.releaseConnection();
}
return buffer.toString();
}
}
补充
package com.chinainsurance.application.common;
public class BeanManager {
private static final BeanManager instance = new BeanManager();
private SpringContainer springContainer;
private BeanManager() {
}
public static BeanManager getInstance() {
return instance;
}
public void setSpringContainer(SpringContainer springContainer) {
this.springContainer = springContainer;
}
private SpringContainer getSpringContainer() {
return springContainer;
}
public static Object getBean(String name) {
return getInstance().getSpringContainer().getComponent(name);
}
}
package com.chinainsurance.application.common.https;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.HttpClientError;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
public class MySecureProtocolSocketFactory implements
SecureProtocolSocketFactory {
private SSLContext sslContext = null;
public MySecureProtocolSocketFactory() {
}
private static SSLContext createEasySSLContext() {
try {
SSLContext context = SSLContext.getInstance("SSL");
context.init(null, new TrustManager[] { new MyX509TrustManager() },
null);
return context;
} catch (Exception e) {
throw new HttpClientError(e.toString());
}
}
private SSLContext getSSLContext() {
if (this.sslContext == null) {
this.sslContext = createEasySSLContext();
}
return this.sslContext;
}
public Socket createSocket(String host, int port, InetAddress clientHost,
int clientPort) throws IOException, UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(host, port,
clientHost, clientPort);
}
public Socket createSocket(final String host, final int port,
final InetAddress localAddress, final int localPort,
final HttpConnectionParams params) throws IOException,
UnknownHostException, ConnectTimeoutException {
if (params == null) {
throw new IllegalArgumentException("Parameters may not be null");
}
int timeout = params.getConnectionTimeout();
if (timeout == 0) {
return createSocket(host, port, localAddress, localPort);
} else {
return ControllerThreadSocketFactory.createSocket(this, host, port,
localAddress, localPort, timeout);
}
}
public Socket createSocket(String host, int port) throws IOException,
UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(host, port);
}
public Socket createSocket(Socket socket, String host, int port,
boolean autoClose) throws IOException, UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(socket, host,
port, autoClose);
}
}