Apache HttpClient调用WebService

引言

公司一直在用一个叫做SOAPUI的付费软件在做后端服务测试,这个软件是按年按人头收费的。SOAPUI是非常好用,基本不用写什么代码,简单的几步UI的操作就可以调用后台服务。
我有时候在想,如果哪一天这个软件涨价了,昂贵的使管理层不愿意购买了,或者到了一个新环境不用这个软件了,如何搭建后端服务的测试框架呢?经过一段时间的研究,发现Apache HTTPClient很适合作为Web服务调用的工具类。不过呢,这篇文章仅涉及HttpClient的使用,不涉及测试框架的搭建。

环境准备

  1. 创建一个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文件:
在这里插入图片描述

  1. 创建HttpClientDemo类
  2. 执行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()));
			}
  1. 在本地部署一个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>
  1. 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
在这里插入图片描述写了十多篇文章了,就是没人加关注,可怜我学富五车!!!
希望看到这篇文章的您成为我的第一个粉丝哦!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值