需要的jar
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.xml.rpc</groupId>
<artifactId>javax.xml.rpc-api</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.2</version>
</dependency>
java 代碼
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
public static void main(String[] args) {
try {
String url = "http://localhost:9000/HelloWorld?wsdl";
//直接引用遠程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName("http://example/","sayHelloWorldFrom"));//命名空間url 和方法名字
// call.addParameter("from", org.apache.axis.encoding.XMLType.XSD_STRING,
// javax.xml.rpc.ParameterMode.IN);//接口的參數
// 參數名, 參數類型String, IN 或 OUT
call.addParameter("arg0", XMLType.XSD_STRING, ParameterMode.IN); // 這里參數不能寫參數名, arg0 代表第一個參數
call.setReturnType(XMLType.XSD_STRING);//設置返回類型
String result = (String)call.invoke(new Object[]{"xxx"});
//給方法傳遞參數,並且調用方法
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
方式 2 springboot
依賴
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.4</version>
</dependency>
private static void test() {
// 創建動態客戶端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:9000/HelloWorld?wsdl");
// 需要密碼的情況需要加上用戶名和密碼
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
try {
// invoke("方法名",參數1,參數2,參數3....);
Object[] helloWorlds = client.invoke("sayHelloWorldFrom", "xxx");
for (Object o : helloWorlds) {
System.out.println(o);
}
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
方式3 : httpClient 方式
1. 安裝SoapUi 獲取 xml
https://blog.csdn.net/w0002399/article/details/82051404
2. 依賴
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.3.2</version>
</dependency>
3. 代碼
public static void main(String[] args) {
// 拼接xml soapui 導出
String orderSoapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <tem:HelloWorld/>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>";
// url
String url = "http://localhost:9000/HelloWorld/?wsdl";
//采用SOAP1.1調用服務端,這種方式能調用服務端為soap1.1和soap1.2的服務
doPost(url, orderSoapXml, "");
}
public static String doPost(String postUrl, String soapXml,
String soapAction) {
String retStr = "";
// 創建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(postUrl);
// 設置請求和傳輸超時時間
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(30000)
.setConnectTimeout(30000).build();
httpPost.setConfig(requestConfig);
try {
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
httpPost.setHeader("SOAPAction", soapAction);
StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient
.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印響應xml內容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
log.info("response:" + retStr);
}
// 釋放資源
closeableHttpClient.close();
} catch (Exception e) {
log.error("exception in doPostSoap1_1", e);
}
return retStr;
}

