From ff0dbbba75b01a385038d0a791cc8c58e900d89b Mon Sep 17 00:00:00 2001 From: tangfutang Date: Tue, 2 Jul 2019 09:43:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=B9=8F=E8=BF=9C=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=AD=A3=E5=BC=8F=E7=8E=AF=E5=A2=83=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src_tenwa/com/tenwa/httpclient/PyUtils.java | 254 ++++++++++---------- 1 file changed, 131 insertions(+), 123 deletions(-) diff --git a/src_tenwa/com/tenwa/httpclient/PyUtils.java b/src_tenwa/com/tenwa/httpclient/PyUtils.java index feb311628..a8d73d54a 100644 --- a/src_tenwa/com/tenwa/httpclient/PyUtils.java +++ b/src_tenwa/com/tenwa/httpclient/PyUtils.java @@ -1,123 +1,131 @@ -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 { - 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); - 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")); - } - } - -} +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"); + System.out.println("转换后的wsdl:" + theString); + 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")); + } + } + +}