还是那句话,如果不是工作上要用都2022年了,谁还会用这种方式去调用接口,无奈还是特此记录一下吧 axis调用参考这位博主https://blog.csdn.net/qq_33236248/article/details/80436688
一 相关依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.0.0</version>
</dependency>
<!-- axis -->
<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>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.4</version>
</dependency>
<!-- mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
二 代码demo
package com.ayx.demo;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import javax.xml.namespace.QName;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class WebServiceDemo {
public static void test2(String[] args) {
String reqData = "<?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" +
" <getSupportCityDataset xmlns=\"http://WebXml.com.cn/\">\n" +
" <theRegionCode>31110</theRegionCode>\n" +
" </getSupportCityDataset>\n" +
" </soap12:Body>\n" +
"</soap12:Envelope>";
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
CloseableHttpClient httpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx");
httpPost.setHeader("Content-Type", "text/xml;charset=utf-8");
StringEntity stringEntity = new StringEntity(reqData, StandardCharsets.UTF_8);
httpPost.setEntity(stringEntity);
try {
CloseableHttpResponse response = httpClient.execute(httpPost);
System.out.println(response);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
StringBuilder sb = new StringBuilder();
byte[] bytes = new byte[8024];
int len = -1;
while ((len = content.read(bytes)) != -1) {
String s = new String(bytes, 0, len);
sb.append(s);
}
System.out.println(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main2(String[] args) {
String url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx";
String reqData = "<?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" +
" <getSupportCityDataset xmlns=\"http://WebXml.com.cn/\">\n" +
" <theRegionCode>31110</theRegionCode>\n" +
" </getSupportCityDataset>\n" +
" </soap12:Body>\n" +
"</soap12:Envelope>";
try {
URL url1 = new URL(url);
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-type","text/xml;charset=utf-8");
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(reqData.getBytes(StandardCharsets.UTF_8));
InputStream inputStream = connection.getInputStream();
byte[] bytes = new byte[8024];
int len = -1;
StringBuilder res = new StringBuilder();
if (connection.getResponseCode()==200){
while ((len = inputStream.read(bytes)) != -1) {
res.append(new String(bytes, 0, len));
}
System.out.println(res);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
String encodingStyle = "utf-8";
String endpoint = "http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx?wsdl";
String targetNamespace = "http://WebXml.com.cn/";
String soapActionURI = "http://WebXml.com.cn/getCharFonts";
String method = "getCharFonts";
String[] paramNames = {"byFontsLength"};
Integer[] paramValues = {1};
Service service = new Service();
Call call = (Call) service.createCall();
call.setSOAPActionURI(soapActionURI);
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setEncodingStyle(encodingStyle);
call.setOperationName(new QName(targetNamespace,method));
call.setUseSOAPAction(true);
call.addParameter(new QName(targetNamespace,paramNames[0]),
org.apache.axis.encoding.XMLType.XSD_INTEGER,
javax.xml.rpc.ParameterMode.IN);
call.setReturnClass(java.lang.String[].class);
String[] result = (String[]) call.invoke(new Object[] {paramValues[0]});
System.out.println("result is " + Arrays.toString(result));
if (result != null && result.length > 0) {
for (int i = 0; i < result.length; i++) {
System.out.println(result[i]);
}
}
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
|