Java调用webservice
1、webService依赖
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
2、调用方式
1、方式一
@GetMapping("/getWeb1")
public String getWeb1() throws ServiceException, RemoteException {
String endpoint = "http://172.10.10.143:44345/MainInst.asmx";
String soapaction = "http://tempuri.org/";
String method = "GetVal";
org.apache.axis.client.Service service = new org.apache.axis.client.Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName(new QName(soapaction, method));
call.addParameter(new QName(soapaction, "tag"),
org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
call.setUseSOAPAction(true);
call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_STRING);
call.setSOAPActionURI(soapaction + method);
call.setProperty(org.apache.axis.MessageContext.HTTP_TRANSPORT_VERSION, HTTPConstants.HEADER_PROTOCOL_V11);
String xmlStr = (String) call.invoke(new Object[]{"tag99"});
return xmlStr;
}
2、方式二
@GetMapping("/getWeb2")
public String getWeb3() {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
" <soap12:Body>\n" +
" <GetVal xmlns=\"http://tempuri.org/\">\n" +
" <tag>tag99</tag>\n" +
" </GetVal>\n" +
" </soap12:Body>\n" +
"</soap12:Envelope>";
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("http://172.10.10.143:44345/MainInst.asmx");
httpPost.setHeader("Content-Type", "text/xml; charset=UTF-8");
CloseableHttpResponse response = null;
try {
StringEntity entity = new StringEntity(xml, "UTF-8");
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
System.out.println("响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "SUCCESS";
}