131 lines
4.5 KiB
Java
131 lines
4.5 KiB
Java
package com.tenwa.httpclient;
|
||
|
||
|
||
|
||
import org.apache.commons.httpclient.params.HttpClientParams;
|
||
import org.apache.commons.io.IOUtils;
|
||
import org.codehaus.xfire.client.Client;
|
||
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;
|
||
import org.w3c.dom.Document;
|
||
|
||
import javax.net.ssl.HttpsURLConnection;
|
||
import javax.xml.bind.JAXBContext;
|
||
import javax.xml.bind.Unmarshaller;
|
||
import java.io.ByteArrayInputStream;
|
||
import java.io.ByteArrayOutputStream;
|
||
import java.io.StringReader;
|
||
import java.net.URL;
|
||
import java.net.URLConnection;
|
||
import java.util.zip.ZipEntry;
|
||
import java.util.zip.ZipInputStream;
|
||
|
||
/**
|
||
* 鹏元工具类
|
||
*/
|
||
public class PyUtils {
|
||
|
||
/**
|
||
* 发起webservice请求
|
||
*
|
||
* @param urlStr
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static String requestApi(String urlStr, String method, Object[] params) throws Exception {
|
||
//System.setProperty("javax.net.debug", "all");
|
||
String result = "";
|
||
try {
|
||
//调用webservice
|
||
URL url = new URL(urlStr);
|
||
Client client = null;
|
||
URLConnection conn = null;
|
||
if (urlStr.startsWith("https")) {
|
||
/**
|
||
* https 调用示例
|
||
*/
|
||
conn = (HttpsURLConnection) url.openConnection();
|
||
((HttpsURLConnection) conn).setSSLSocketFactory(PySSLContextUtil.createCustomerSSLContext().getSocketFactory());
|
||
|
||
} else {
|
||
/**
|
||
* http 调用示例
|
||
*/
|
||
conn = url.openConnection();
|
||
|
||
}
|
||
// 设置连接超时
|
||
conn.setConnectTimeout(5000);
|
||
// 设置读取超时
|
||
conn.setReadTimeout(5000);
|
||
|
||
String theString = IOUtils.toString(conn.getInputStream(), "UTF-8");
|
||
//正式环境经过前置nginx转发后https就变为了http请求,所以webservice wsdl中的https地址做一次替换为http操作
|
||
theString = theString.replaceAll("https://www.pycredit.com:8443", "http://www.pycredit.com:8443");
|
||
//theString = theString.replaceAll("https://test.pycredit.com:6443", "http://www.pycredit.com:6443");
|
||
client = new Client(new ByteArrayInputStream(theString.getBytes()), null);
|
||
|
||
// client = new Client(conn.getInputStream(), null);
|
||
// 设置
|
||
HttpClientParams httpClientParams = new HttpClientParams();
|
||
// 单位毫秒: 这个超时时间是 等待数据返回的时间
|
||
httpClientParams.setSoTimeout(20000);
|
||
// 设置从 HTTP connection manager 获取一个HTTP connection 的超时时间
|
||
httpClientParams.setConnectionManagerTimeout(10000L);
|
||
// xfire client支持 避免 'Expect: 100-continue' handshake
|
||
httpClientParams.setBooleanParameter(HttpClientParams.USE_EXPECT_CONTINUE, Boolean.FALSE);
|
||
client.setProperty(CommonsHttpMessageSender.HTTP_CLIENT_PARAMS, httpClientParams);
|
||
//xfire client支持 取消长连接
|
||
client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true");
|
||
|
||
//得到子报告结果
|
||
Object[] results = client.invoke(method, params);
|
||
|
||
if (results[0] instanceof Document) {
|
||
Document doc = (Document) results[0];
|
||
result = doc.getFirstChild().getFirstChild().getNodeValue();
|
||
} else {
|
||
result = (String) results[0];
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 解析报文(仅供参考)
|
||
*
|
||
* @param bodyStr
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static WebServiceResult parseBody(String bodyStr) throws Exception {
|
||
JAXBContext context = JAXBContext.newInstance(WebServiceResult.class);
|
||
Unmarshaller unmarshaller = context.createUnmarshaller();
|
||
WebServiceResult result = (WebServiceResult) unmarshaller.unmarshal(new StringReader(bodyStr));
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 解压缩
|
||
*
|
||
* @param data
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static void decompress(byte[] data) throws Exception {
|
||
|
||
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
||
ZipInputStream zis = new ZipInputStream(bais);
|
||
ZipEntry ze = null;
|
||
while ((ze = zis.getNextEntry()) != null) {
|
||
System.out.println("文件名:" + ze.getName());
|
||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||
IOUtils.copy(zis, baos);
|
||
zis.closeEntry();
|
||
System.out.println("文件内容:" + new String(baos.toByteArray(), "GBK"));
|
||
}
|
||
}
|
||
|
||
}
|