SOAP-UI
SoapUI是為Windows、Mac和Linux作業系統開發的免費程式。作為測試SOAP(簡單對象通路協定)和REST(代表性狀态傳輸)web協定的一種方法,SoapUI提供了執行許多不同測試所需的工具和功能。這些測試包括功能測試、負載測試、安全測試和模拟。作為一個開源程式,SoapUI是市場上第一個同類的API測試工具,并且在開發人員的幫助下已經形成了今天的産品。
位址:https://www.soapui.org/downloads/latest-release/,https://www.soapui.org/downloads/soapui/soapui-os-older-versions/
生成SOAP
SOAP請求封包:
POST http://192.168.3.50:9199/demo/cxf/userWebService HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
Content-Length: 299
Host: 192.168.3.50:9199
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.5 (Java/17.0.2)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.demo.com">
<soapenv:Header/>
<soapenv:Body>
<ser:userInfo>
<!--Optional:-->
<username>?</username>
</ser:userInfo>
</soapenv:Body>
</soapenv:Envelope>
SOAP響應封包
HTTP/1.1 200
Content-Type: text/xml;charset=UTF-8
Content-Length: 208
Date: Fri, 11 Mar 2022 11:00:04 GMT
Keep-Alive: timeout=60
Connection: keep-alive
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:userInfoResponse xmlns:ns2="http://service.demo.com">
<String>?</String>
</ns2:userInfoResponse>
</soap:Body>
</soap:Envelope>
HTTPClient調用WebService服務
pom.xml
<!-- HttpClient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1</version>
</dependency>
用戶端調用
package com.what21.demo.service;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class WebServiceClientDemo {
/**
* @param args
* @return
*/
public static void main(String[] args) {
String url = "http://192.168.3.50:9199/demo/cxf/userWebService";
String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.demo.com\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <ser:userInfo>\n" +
" <!--Optional:-->\n" +
" <username>?</username>\n" +
" </ser:userInfo>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>";
String result = null;
HttpEntity httpEntity = null;
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
// 建立POST
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(getRequestConfig());
// 設定頭參數
Header contentTypeHeader = new BasicHeader("Content-Type", "text/xml;charset=UTF-8");
httpPost.setHeader(contentTypeHeader);
Header soapActionHeader = new BasicHeader("SOAPAction", "");
httpPost.setHeader(soapActionHeader);
// 頭設定封包和通訊格式
StringEntity stringEntity = new StringEntity(xml, ContentType.TEXT_XML);
stringEntity.setContentEncoding("UTF-8");
httpPost.setEntity(stringEntity);
// 執行
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
EntityUtils.consume(httpEntity);
} catch (IOException e) {
// http請求釋放資源異常
e.printStackTrace();
}
}
System.out.println(result);
}
/**
* @return
*/
public static RequestConfig getRequestConfig() {
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(180 * 1000) // 連接配接逾時時間
.setConnectTimeout(180 * 1000) // 請求逾時時間
.setConnectionRequestTimeout(180 * 1000) // 連接配接請求逾時時間
.setRedirectsEnabled(true) // 重定向開關
.build();
return requestConfig;
}
}