引言
公司一直在用一个叫做SOAPUI的付费软件在做后端服务测试,这个软件是按年按人头收费的。SOAPUI是非常好用,基本不用写什么代码,简单的几步UI的操作就可以调用后台服务。
我有时候在想,如果哪一天这个软件涨价了,昂贵的使管理层不愿意购买了,或者到了一个新环境不用这个软件了,如何搭建后端服务的测试框架呢?经过一段时间的研究,发现Apache HTTPClient很适合作为Web服务调用的工具类。不过呢,这篇文章仅涉及HttpClient的使用,不涉及测试框架的搭建。
环境准备
- 创建一个Maven project, POM文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stone.http</groupId>
<artifactId>httpclientdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>httpclientdemo</name>
<description>demo project for http,client</description>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
</dependencies>
</project>
在Maven Dependencies下面有一下jar文件:
- 创建HttpClientDemo类
- 执行HTTP Get方法:访问搜狗主页,并打印response body
try(CloseableHttpClient client = HttpClients.createDefault()){
HttpGet get = new HttpGet("https://www.sogou.com");
CloseableHttpResponse response = client.execute(get);
System.out.println(String.format("Status code is %s", response.getStatusLine().getStatusCode()));
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer content = new StringBuffer();
String line;
while((line = reader.readLine()) != null) {
content.append(line);
content.append("\n");
}
reader.close();
EntityUtils.consume(response.getEntity());
System.out.println(String.format("Body is \n %s", content.toString()));
}
- 在本地部署一个soapservice,名字叫做“HelloWorldService.asmx”, 调用参数和响应如下
Request
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:HelloWorld>
<!--Optional:-->
<tem:greeting>Stone</tem:greeting>
</tem:HelloWorld>
</soapenv:Body>
</soapenv:Envelope>
Response
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<HelloWorldResponse xmlns="http://tempuri.org/">
<HelloWorldResult>
<ResponseCode>0</ResponseCode>
<ResponseText>success</ResponseText>
<Message>Hello Stone!</Message>
</HelloWorldResult>
</HelloWorldResponse>
</soap:Body>
</soap:Envelope>
- Http Post 方法:调用SOAPservice
try(CloseableHttpClient client = HttpClients.createDefault()){
HttpPost post = new HttpPost("http://localhost/HelloWorldService.asmx");
post.setHeader("User-Agent", "Apache-HttpClient/4.1.1 (java 1.8)");
post.setHeader("SOAPAction", "http://tempuri.org/HelloWorld");
post.setHeader("Content-Type", "text/xml;charset=UTF-8");
String request = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\r\n" +
" <soapenv:Header/>\r\n" +
" <soapenv:Body>\r\n" +
" <tem:HelloWorld>\r\n" +
" <!--Optional:-->\r\n" +
" <tem:greeting>Stone</tem:greeting>\r\n" +
" </tem:HelloWorld>\r\n" +
" </soapenv:Body>\r\n" +
"</soapenv:Envelope>";
StringEntity entity = new StringEntity(request);
post.setEntity(entity);
CloseableHttpResponse res = client.execute(post);
System.out.println(EntityUtils.toString(res.getEntity()));
}
调用SOAPservice比较要注意的地方就是在Header里面加“SOAPAction”:
post.setHeader(“SOAPAction”, “http://tempuri.org/HelloWorld”);
Request需要从WSDL里面去解析,这次demo里面我使用SOAPUI直接导入wsdl得到的request
写了十多篇文章了,就是没人加关注,可怜我学富五车!!!
希望看到这篇文章的您成为我的第一个粉丝哦!