经过一天的艰苦奋斗,发现了一篇宝藏文章,给我解决了头疼的webService调用(本人小白O(∩_∩)O哈哈~) ,为了记忆以及分享----码了这第一次的csdn!
以下为某博客大佬的知识通道~
https://www.iteye.com/blog/keepgoingxjw-2386396
https://www.iteye.com/blog/keepgoingxjw-2386396
我碰见了两个坑,一是参数没有对应,二是xml头不对。
原先我的代码
public void callWebserviceASMX(){
//获取webservice接口地址
String url = "http://localhost:666/....asmx";
//提供方的路径
String soapaction = "...";
org.apache.axis.client.Service service = new org.apache.axis.client.Service();
try {
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
//设置要调用的方法 method为方法名
call.setOperationName(new QName(soapaction,method));
//添加参数,切记
call.addParameter("参数", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("参数", XMLType.XSD_INT, ParameterMode.IN);
//也可以这么玩
*//*call.addParameter(new QName(soapaction , "参数"),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction , "参数"),
XMLType.XSD_INT,
javax.xml.rpc.ParameterMode.IN);*//*
//设置要返回的数据类型
call.setReturnType(new QName(soapaction,method), String.class);
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapaction+method);
//调用方法并传递参数
int result = (int) call.invoke(new Object[]{verification,155});
System.out.println("result is:::"+result);
} catch (ServiceException | RemoteException e) {
e.printStackTrace();
logger.info("完成状态远程接口(setProductionPlanInfo)调用异常。"+ e);
}
}
结果发现拼装的xml标准不一样,网上说有阿帕奇的、微软的、IBM的。。。我不懂
大佬的做法大家可以参考一下~是用http服务调用的。
public void setPlanIdToWebService(String planId){
String storeInfoUrl = "http://localhost:666/....asmx";
String contentType = "text/xml; charset=utf-8";//soap1.1 的conetentType
String soapAction = ".../接口方法";
String soapXml = "请求的xml格式并加数据";
try {
InputStream input = HomePageService.soap(storeInfoUrl, soapXml, soapAction, contentType);
} catch (Exception e){
e.printStackTrace();
}
//下面是解析 返回的 XML
/*SAXReader saxReader = new SAXReader();
Document document = null;
try {
document = saxReader.read(input);
Element root = document.getRootElement();
Dom4jUtils.getNodes(root);
Element body = root.element("Body");
Element getSupportCityResponse = body.element("getSupportCityResponse");
Element getSupportCityResult = getSupportCityResponse.element("getSupportCityResult");
List<Element> strings = getSupportCityResult.elements();
for(Element e : strings){
System.out.println(e.getTextTrim());
}
} catch (DocumentException e) {
e.printStackTrace();
}*/
}
public static InputStream soap(String url,String soapXml,String soapAction,String ContentType) {
InputStream soapResponseData = null;
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(url);
//设置请求超时时间 和 传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeOut)
.setConnectTimeout(connectTimeOut).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type",ContentType);
httpPost.setHeader("SOAPAction",soapAction);
StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = null;
try {
response = closeableHttpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity !=null){
//打印响应内容
byte[] bytes = EntityUtils.toByteArray(httpEntity);
soapResponseData =new ByteArrayInputStream(bytes);
log.info("---请求webService之后的返回值是----"+new String(bytes));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return soapResponseData;
}