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;
}
}