使用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主体数据的"<"与">"替换成"&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);
    }
}

原文地址:https://www.cnblogs.com/zw520ly/p/5892439.html