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主体数据的"<"与">"替换成"&lt;"与"&gt;"
        String strData = new String(data.get("data").replace("<", "&lt;")).replace(">", "&gt;");
        // 请求的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)的更多相关文章

  1. 使用HttpClient工具类测试Http接口

    一.httpClient模拟客户端 import java.util.ArrayList;import java.util.Iterator;import java.util.List;import ...

  2. 【SoapUI、Postman、WebServiceStudio、Jmeter】接口测试工具结合测试webservice接口(发送XML格式参数)

    目录: SoapUI测试webservice接口,发送XML格式参数 Postman测试webservice接口,发送XML格式参数 WebServiceStudio.exe测试webservice接 ...

  3. 使用URL工具类调用webservice接口(soap)与http接口的实现方式

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  4. 通过Httpclient工具类,实现接口请求

    package luckyweb.seagull.util; import org.apache.http.NameValuePair; import org.apache.http.client.e ...

  5. python实现建立soap通信(调用及测试webservice接口)

    实现代码如下: #调用及测试webservice接口 import requests class SoapConnect: def get_soap(self,url,data): r = reque ...

  6. 『动善时』JMeter基础 — 50、使用JMeter测试WebService接口

    目录 1.什么是WebService 2.WebService和SOAP的关系 3.什么是WSDL 4.测试WebService接口前的准备 (1)如何判断是WebService接口 (2)如何获取W ...

  7. 『动善时』JMeter基础 — 51、使用JMeter测试WebService接口

    目录 1.什么是WebService 2.WebService和SOAP的关系 3.什么是WSDL 4.测试WebService接口前的准备 (1)如何判断是WebService接口 (2)如何获取W ...

  8. Python之测试webservice接口

    前段时间学习了Python操作http接口,觉得挺容易的.最近项目组也有接触webservice接口,心里想想是否Python也可以操作这类接口.于是利用伟大的度娘,花了6个小时研究出来了,所以迫不及 ...

  9. Java开发小技巧(五):HttpClient工具类

    前言 大多数Java应用程序都会通过HTTP协议来调用接口访问各种网络资源,JDK也提供了相应的HTTP工具包,但是使用起来不够方便灵活,所以我们可以利用Apache的HttpClient来封装一个具 ...

随机推荐

  1. [转] 你真的会写单例模式吗——Java实现

    你真的会写单例模式吗——Java实现 原文:http://www.tuicool.com/articles/MBrUfy6 单例模式可能是代码最少的模式了,但是少不一定意味着简单,想要用好.用对单例模 ...

  2. python hmac-sha1

    def getSignature(Token,paramss1): s = getParam(paramss1) print(s) # for k, v in paramss1.items(): # ...

  3. ios发布笔录

    需要一张1024x1024的icon 发布尺寸4.7英寸  1334x7505.5英寸 2208-12424英寸  1136-6403.5英寸 960-640ipad  2048x1536 视频 ip ...

  4. 用sql实现汉字转拼音

    有时我们会需要将汉字转为拼音,例如需要将省市转为拼音后当做编码存储(尽管国家有统一的标识码,但有时候我们还是会用到),网络上也有工具提供汉字转拼音的功能,但各有优劣,一般转拼音后还会存在带声调的字母, ...

  5. OpenCV FileStorage 使用记录

    FileStorage OpenCV 中的 FileStorage 类能够读写硬盘中的.xml和.yaml文件,这里我们只讨论对 .xml 的以下几种操作: 写入(FileStorage::WRITE ...

  6. mysql 分组按条件统计

    百度经验 COUNT(CASE WHEN (S.rank = 1) THEN S.loanContractId END ) AS 'MZ',  //根据loanContractId 分组,并统计ran ...

  7. android 调用.NET WebServices

    下载Ksoap2.jar, import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.*;import org.ksoap2.tra ...

  8. MVC修改文件上传大小问题

    在web.config文件的system.web节点的httpRuntime节点加入 maxRequestLength="2147483647" executionTimeout= ...

  9. UIView 和 CALayer 的区别和联系

    UIView是在/System/Library/Frameworks/UIKit.framework定义,也就是处于Cocoa Touch层. CALyer是在/System/Library/Fram ...

  10. linux 安装mysql数据库

    Ubuntu上安装MySQL非常简单,只需要打开终端,几条命令就可以完成. 1. sudo apt-get install mysql-server 2. apt-get isntall mysql- ...