使用HttpClient工具类测试WebService接口(soap)
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class HttpClientTest {
// 使用Excel数据驱动取数据
@DataProvider(name = "datapro")
public Iterator<Object[]> Data() {
return new ExcelDataProvider("SoapTest", "testSoap");
}
@Test(dataProvider = "datapro")
public void httpPost(Map<String, String> data) throws IOException {
// 对传输数据进行加密,这里使用SHA-1算法加密
SoapKey soapKey = new SoapKey();
String key = soapKey.getMessageDigest(data.get("data"), "SHA-1");
// 将请求XML主体数据的"<"与">"替换成"<"与">"
String strData = new String(data.get("data").replace("<", "<")).replace(">", ">");
// 请求的XML数据,即请求体
String soapReuqest = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.ws.gpo.yy.com/\">"
+ "<soapenv:Header/>" + "<soapenv:Body><ser:sendRecv4Supplier><!--Optional:--><sUser>"
+ data.get("user") + "</sUser><!--Optional:--><sPwd>" + data.get("passwd")
+ "</sPwd><!--Optional:--><sJgbm>" + data.get("jgbm")
+ "</sJgbm><!--Optional:--><sVersion>1.0.0.0</sVersion><!--Optional:--><sXxlx>" + data.get("msgType")
+ "</sXxlx><!--Optional:--><sSign>" + key + "</sSign><!--Optional:-->" + "<xmlData>" + strData
+ "</xmlData>" + "</ser:sendRecv4Supplier></soapenv:Body></soapenv:Envelope>";
// 1.创建httpClient客户端
CloseableHttpClient httpclient = HttpClients.createDefault();
// 2.获取http post
HttpPost httppost = new HttpPost(data.get("urlStr"));
// 3.设置发送请求的字符集编码
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=" + "utf-8");
/*
* // 4.把SOAP请求数据添加到http post方法中,此方式也可实现,处理方式可能有点绕
byte[] by = soapReuqest.getBytes("utf-8");
InputStream inputStream = new ByteArrayInputStream(by, 0, by.length);
//实例化输入流请求实体:使用HttpClient测试Soap接口比较特殊;服务端可能是以IO流的形式接收数据的,此处先作此定论,后续再做研究
InputStreamEntity reqEntity = new InputStreamEntity(inputStream,by.length);
*/
//设置http请求实体,将请求的String数据转换成StringEntity实体,一定要指定字符集编码
StringEntity reqEntity = new StringEntity(soapReuqest,"utf-8");
httppost.setEntity(reqEntity);
// 5.执行http post请求
HttpResponse response = httpclient.execute(httppost);
// 6.获取服务端返回的状态码
int statuscode = response.getStatusLine().getStatusCode();
// 7.获取服务器的返回实体
HttpEntity entity = response.getEntity();
String responseMsg = EntityUtils.toString(entity);
System.out.println("接口:" + data.get("msgType") + ":返回的状态码与响应结果:" + statuscode + ":" + responseMsg);
}
}
使用HttpClient工具类测试WebService接口(soap)的更多相关文章
- 使用HttpClient工具类测试Http接口
一.httpClient模拟客户端 import java.util.ArrayList;import java.util.Iterator;import java.util.List;import ...
- 【SoapUI、Postman、WebServiceStudio、Jmeter】接口测试工具结合测试webservice接口(发送XML格式参数)
目录: SoapUI测试webservice接口,发送XML格式参数 Postman测试webservice接口,发送XML格式参数 WebServiceStudio.exe测试webservice接 ...
- 使用URL工具类调用webservice接口(soap)与http接口的实现方式
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...
- 通过Httpclient工具类,实现接口请求
package luckyweb.seagull.util; import org.apache.http.NameValuePair; import org.apache.http.client.e ...
- python实现建立soap通信(调用及测试webservice接口)
实现代码如下: #调用及测试webservice接口 import requests class SoapConnect: def get_soap(self,url,data): r = reque ...
- 『动善时』JMeter基础 — 50、使用JMeter测试WebService接口
目录 1.什么是WebService 2.WebService和SOAP的关系 3.什么是WSDL 4.测试WebService接口前的准备 (1)如何判断是WebService接口 (2)如何获取W ...
- 『动善时』JMeter基础 — 51、使用JMeter测试WebService接口
目录 1.什么是WebService 2.WebService和SOAP的关系 3.什么是WSDL 4.测试WebService接口前的准备 (1)如何判断是WebService接口 (2)如何获取W ...
- Python之测试webservice接口
前段时间学习了Python操作http接口,觉得挺容易的.最近项目组也有接触webservice接口,心里想想是否Python也可以操作这类接口.于是利用伟大的度娘,花了6个小时研究出来了,所以迫不及 ...
- Java开发小技巧(五):HttpClient工具类
前言 大多数Java应用程序都会通过HTTP协议来调用接口访问各种网络资源,JDK也提供了相应的HTTP工具包,但是使用起来不够方便灵活,所以我们可以利用Apache的HttpClient来封装一个具 ...
随机推荐
- ActiveMQ in Action(6) - Features
关键字: activemq 2.6 Features ActiveMQ包含了很多功能强大的特性,下面简要介绍其中的几个.2.6.1 Exclusive Consumer Queue中的消息 ...
- shell的入门
shell :弱类型. 解释型语言 从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 ...
- 【实验室笔记】serialport的readline()方法
在最近的小项目中,单片机中断优先级的问题,串口发送到上位机的数据有时会出现发送的数据被中断打断的问题. 于是,在上位机机上就容易出现错误,原来读取的方法是read()的方法,反复修改发送数据的格式依然 ...
- Github命令详解
Git bash: ***创建本地版本库: $ cd d: $ mkdir git $ cd git $ mkdir test $ git init //初始化本地库 ***创建文件并添加到版本库 ...
- 2015 QQ最新登录算法
首先还是取得验证码,抓包可得:http://check.ptlogin2.qq.com/check?regmaster=&pt_tea=1&uin=2630366651&app ...
- 第九章 观察者模式 OBSERVER
当对象发生改变时,应该使客户得到通知,然后,让客户查询对象的新状态. 其目的是在对象之间(目标对象和客户对象),定义了一个一对多的依赖关系,从而一个对象状态发生改变时,所有依赖这个对象的对象都能得到通 ...
- svn中“clean up”死循环问题解决办法
SVN在使用update命令时,提示使用"clean up "命令,在使用clean up命令时报错"Previous operation has not finishe ...
- 描述符和property内建函数
首先我们搞清楚__getattr__ ,__get__ 和 __getattribute__ 作用的不同点. __getattr__在授权中会用到. __getattribute__ 当要访问属性时 ...
- redis5--set的操作
Set集合类型(1)介绍redis的set是string类型的无序集合set元素最大可以包含(2的32次方-1)个元素关于set集合类型除了基本的添加删除操作,其它有用的操作还包含集合的取并集(uni ...
- 重读The C programming Lanuage 笔记二:运算符优先级
运算符的优先级和结合性有明确的规定,但是,除少数例外情况外,表达式的求值次序没有定义,甚至某些有副作用的子表达式也没有定义. 也就是说运算符的定义保证了其操作数按某一特定的顺序求值,否则具体实现可以自 ...