使用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来封装一个具 ...
随机推荐
- jdk7 HashSet和HashMap源码分析
先来看看HashMap的一些成员变量以及他们的含义 /** * The default initial capacity - MUST be a power of two. */ static fin ...
- com.google.common.collect.Lists#transform使用注意
/** * Returns a list that applies {@code function} to each element of {@code * fromList}. The return ...
- mysql最大连接数问题
进入mysql系统就, 查询最大连接数:show variables like 'max_connections'; 修改最大连接数:set global max_connections=1000;
- How to use php serialize() and unserialize()
A PHP array or object or other complex data structure cannot be transported or stored or otherwise u ...
- php +mysql 添加 删除 修改 insert into delete update
INSERT INTO 插入数据库 $sql = "INSERT INTO subject (uid,fun,title) VALUES (3,88,'语文')"; $query ...
- Spring MVC如何进行JSON数据的传输与接受
本篇文章写给刚接触SpingMVC的同道中人,虽然笔者本身水平也不高,但聊胜于无吧,希望可以给某些人带来帮助 笔者同时再次说明,运行本例时,需注意一些配置文件和网页脚本的路径,因为笔者的文件路径与读者 ...
- visual SVN 反编译破解
今天发现visual SVN 过期了.网上搜索了一下,发现了下面的贴子. http://www.heiqu.com/show-71200-1.html 一看是用.Net写的,大喜,破解就太简单了.本来 ...
- ACdream 1063 平衡树
写的很丑的字典树.听王大神的话 需要改进. #include<stdio.h> #include<string.h> #include<math.h> #incl ...
- selenium webdriver学习-怎么等待页面元素加载完成
http://blog.csdn.net/aerchi/article/details/8055913 WebDriverWait类和ExpectedCondition
- Java中的Math类的简单实用
System.out.println(Math.PI);//获取PI的值 System.out.println(Math.E);//常量E int min = Math.min(5, 4);//求最小 ...