使用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);
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM