axis1.4客户端调用http形式
Posted lirenzhujiu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了axis1.4客户端调用http形式相关的知识,希望对你有一定的参考价值。
/** * http调用响应结果 * @see [相关类/方法] * @since [云渠道2.0/module] */ public class HttpResp { /** * 调用结果 */ private String result; /** * 接口响应报文 */ private String resp; /** * 开始调用时间 */ private long start; /** * 接口调用耗时 */ private long cost; public String getResult() { return result; } public void setResult(String result) { this.result = result; } public String getResp() { return resp; } public void setResp(String resp) { this.resp = resp; } public long getStart() { return start; } public void setStart(long start) { this.start = start; } public long getCost() { return cost; } public void setCost(long cost) { this.cost = cost; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append(resp); return builder.toString(); } }
import java.io.IOException; import java.util.Map; import java.util.Map.Entry; import org.apache.http.HttpEntity; import org.apache.http.ParseException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; /** * http工具 * @see [相关类/方法] * @since [产品/模块版本] */ public class HttpsUtil { private final static Logger LOGGER = Logger.getLogger(HttpsUtil.class); private static CloseableHttpClient httpClient; private HttpsUtil() { httpClient = HttpClients.createDefault(); } public static HttpsUtil getInstance() { return new HttpsUtil(); } /** * 发送get请求 * @param [参数1] [参数1说明] * @param [参数2] [参数2说明] * @return [返回类型说明] * @throws IOException * @throws ParseException * @exception/throws [违例类型] [违例说明] * @see [类、类#方法、类#成员] */ public HttpResp sendHttpsPost(String url, Map<String, String> header, String body) { return sendHttpsPost(url, header, body, "UTF-8"); } /** * 发送get请求 * @param [参数1] [参数1说明] * @param [参数2] [参数2说明] * @return [返回类型说明] * @throws IOException * @throws ParseException * @exception/throws [违例类型] [违例说明] * @see [类、类#方法、类#成员] */ public HttpResp sendHttpsPost(String url, Map<String, String> header, String body, String charset) { LOGGER.debug("sendHttpsGet(..); url=" + url); LOGGER.debug("sendHttpsGet(..); header=" + header); LOGGER.debug("sendHttpsGet(..); body=" + body); if (url == null || url.trim().equals("")) { LOGGER.error("send https get request failed, url is null"); return null; } HttpResp httpResp = new HttpResp(); long start = System.currentTimeMillis(); httpResp.setStart(start); CloseableHttpResponse httpResponse = null; HttpEntity entity = null; try { HttpPost httpPost = new HttpPost(url); setTimeConfig(httpPost); httpPost.setEntity(new StringEntity(body, charset)); addHttpHeader(httpPost, header); httpResponse = httpClient.execute(httpPost); int statusCode = httpResponse.getStatusLine().getStatusCode(); if (200 == statusCode) { LOGGER.info("success:" + statusCode); } else { LOGGER.info("fail:" + statusCode); } entity = httpResponse.getEntity(); String resp = EntityUtils.toString(entity, charset); httpResp.setResp(resp); } catch (ConnectTimeoutException e) { LOGGER.error(new StringBuffer( "send https post request failed, ConnectTimeoutException ") .toString(), e); e.printStackTrace(); httpResp.setResult("9999"); } catch (IOException e) { LOGGER.error(new StringBuffer( "send https post request failed, IOException ").toString(), e); e.printStackTrace(); httpResp.setResult("9999"); } finally { long cost = System.currentTimeMillis() - start; httpResp.setCost(cost); resourceClosed(httpClient, entity, httpResponse); } return httpResp; } /** * * [关闭连接] [功能详细描述] * @param [参数1] [参数1说明] * @param [参数2] [参数2说明] * @return [返回类型说明] * @exception/throws [违例类型] [违例说明] * @see [类、类#方法、类#成员] */ private void resourceClosed(CloseableHttpClient httpClient, HttpEntity httpEntity, CloseableHttpResponse closeableHttpResponse) { try { if (httpEntity != null) { EntityUtils.consume(httpEntity); } if (closeableHttpResponse != null) { closeableHttpResponse.close(); } } catch (IOException e) { LOGGER.error("https resource closed error",e); } } private void setTimeConfig(HttpRequestBase base) { int soTimeout = 30000; int connTimeout = 30000; int connMgrTimeout = 30000; RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(connTimeout) .setConnectionRequestTimeout(connMgrTimeout) .setSocketTimeout(soTimeout).build(); base.setConfig(requestConfig); } public HttpResp sendHttpsGet(String url, Map<String, String> header, String body) { return sendHttpsGet(url, header, body, "UTF-8"); } public HttpResp sendHttpsGet(String url, Map<String, String> header, String body, String charset) { LOGGER.debug("sendHttpsGet(..); body=" + body); LOGGER.debug("sendHttpsGet(..); header=" + header); LOGGER.debug("sendHttpsGet(..); url=" + url); if (url == null || url.trim().equals("")) { LOGGER.error("send https get request failed, url is null"); return null; } HttpResp httpResp = new HttpResp(); long start = System.currentTimeMillis(); httpResp.setStart(start); HttpEntity entity = null; CloseableHttpResponse httpResponse = null; try { String buildGetUrl = buildGetUrl(url, body); HttpGet httpGet = new HttpGet(buildGetUrl); setTimeConfig(httpGet); addHttpHeader(httpGet, header); httpResponse = httpClient.execute(httpGet); if (httpResponse == null) { return null; } int statusCode = httpResponse.getStatusLine().getStatusCode(); System.out.println("httpstatus:"+statusCode); entity = httpResponse.getEntity(); String resp = EntityUtils.toString(entity, charset); httpResp.setResp(resp); } catch (ConnectTimeoutException e) { LOGGER.error(new StringBuffer( "send https get request failed, ConnectTimeoutException ") .toString(), e); httpResp.setResult("9999"); } catch (IOException e) { LOGGER.error(new StringBuffer( "send https get request failed, IOException ").toString(), e); httpResp.setResult("9999"); } finally { long cost = System.currentTimeMillis() - start; httpResp.setResult("0000"); httpResp.setCost(cost); resourceClosed(httpClient, entity, httpResponse); } return httpResp; } private void addHttpHeader(HttpRequestBase requestBase, Map<String, String> header) { if (header == null || header.isEmpty()) { LOGGER.warn("header is empty"); return; } for (Entry<String, String> entrySet : header.entrySet()) { requestBase.addHeader(entrySet.getKey(), entrySet.getValue()); } } private String buildGetUrl(String url, String body) { url = url.trim(); body = body.trim(); StringBuffer realPath = new StringBuffer(); realPath.append(url); if (!url.contains("?")) { realPath.append("?"); } else { realPath.append("&"); } realPath.append(body); return realPath.toString(); } }
import java.util.HashMap; import java.util.Map; public class Main2 { public static void main(String[] args) { String url = "http://ip:port/xxx"; Map<String, String> header = new HashMap<String, String>(); header.put("SOAPAction", ""); String body = getXmlData2(); System.out.println(body); String chartset = "UTF-8"; HttpResp resp = HttpsUtil.getInstance().sendHttpsPost( url, header, body, chartset); System.out.println(resp.toString()); } public static String getXmlData() { StringBuffer sb = new StringBuffer(); sb.append("<soapenv:Envelope xmlns:soapenv=\\"http://xxxxxx/\\" xmlns:aaa=\\"http://xxxxx/\\">"); sb.append("<soapenv:Header/>"); sb.append("<soapenv:Body>"); sb.append("<aaa>"); sb.append("<bbb>"); sb.append("<id>201805192212345678</sequenceNo>"); sb.append("<type>aaa</srcDeviceType>"); sb.append("<srcDeviceId>1</srcDeviceId>"); sb.append("<loginName>wp</loginName>"); sb.append("</bbb>"); sb.append("</aaa>"); sb.append("</soapenv:Body>"); sb.append("</soapenv:Envelope>"); return sb.toString(); } public static String getXmlData2() { StringBuffer sb = new StringBuffer(); sb.append("<soapenv:Envelope xmlns:soapenv=\\"http://xxxxx/\\" xmlns:ser=\\"http://xxx\\">"); sb.append("<soapenv:Header />"); sb.append("<soapenv:Body>"); sb.append("<ser:remind>"); sb.append("<ser:opDetail>"); sb.append("<![CDATA["); sb.append("<recordInfo>"); sb.append("<fieldInfo>"); sb.append("<fieldChName>流水号</fieldChName>"); sb.append("<fieldEnName>so_serial_code</fieldEnName>"); sb.append("<fieldContent>20150629154117</fieldContent>"); sb.append("</fieldInfo>"); sb.append("<fieldInfo>"); sb.append("<fieldChName>意见</fieldChName>"); sb.append("<fieldEnName>remind_advice</fieldEnName>"); sb.append("<fieldContent>121</fieldContent>"); sb.append("</fieldInfo>"); sb.append("</recordInfo>"); sb.append("]]>"); sb.append("</ser:opDetail>"); sb.append("</ser:remind>"); sb.append("</soapenv:Body>"); sb.append("</soapenv:Envelope>"); return sb.toString(); } }
任何webservice调用都可以用以上模板。
在写的过程中遇到如下问题:no SOAPAction header!
<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <soapenv:Fault> <faultcode xmlns:ns1="http://xml.apache.org/axis/">ns1:Client.NoSOAPAction</faultcode> <faultstring>no SOAPAction header!</faultstring> <detail> <ns2:hostname xmlns:ns2="http://xml.apache.org/axis/">bportal</ns2:hostname> </detail> </soapenv:Fault> </soapenv:Body> </soapenv:Envelope>
1.关于SOAPAction描述:SOAPAction指的就是wsdl调用的方法。具体描述看:https://blog.csdn.net/kthq/article/details/1823686/
2.出现no SOAPAction header问题分析:
我有以下两个wsdl:
和
当我调用remind就出现no SOAPAction header问题,调用userTemplateQuery方法时便没有。原来axis1.4代码中指定,当服务端没有指定soapAction时,客户端调用必须指定,如果服务端指定了soapAction,则客户端调用不用指定。
3.服务端的sopAction=“”的原因是wsdl文件中存在多个方法,所以没有进行指定,等客户端调用指定
4.soapAction指的就是目的地即需要调用的方法,组成部分:soapAction=targetNamespace+方法名。如:我的targetNamespace为" targetNamespace="http://service.interfaces.com",方法名为remind,则soapAction就是:soapAction=http://service.interfaces.com/remind
5.解决办法:在head中设定SOAPAction的值,可为空,也可为URI。所以我在main方法中传入head.put("soapAction","");。再将head传入到httpUtil中去。
以上是关于axis1.4客户端调用http形式的主要内容,如果未能解决你的问题,请参考以下文章
基于Axis1.4的webservice接口开发(接口调用)
基于Axis1.4的webservice接口开发(代码开发)
java webservices 以Axis1.4方式 调用sap webservice接口.