使用HttpClient工具類測試WebService介面(Soap)與Http介面,這裡測試WebService介面,測試http介面實現方式類似

來源:互聯網
上載者:User

標籤:

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.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資料驅動取資料,TestNG的DataProvider註解提供資料非常方便,實現原理不必深究,它會自動調用ExcelDataProvider並返回一個Map給測試方法,如果使用DataProvider的Excel做資料驅動,應將測試方法的參數指定為Map
    @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.設定發送請求的字元集編碼,如果不指定,就會預設以16進位的字元集編碼發送,返回的響應結果就會亂碼
        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);
        InputStreamEntity reqEntity = new InputStreamEntity(inputStream, by.length);
        httppost.setEntity(reqEntity);

        // 5.執行http post請求
        HttpResponse response = httpclient.execute(httppost);
        // 6.擷取服務端返回的狀態代碼
        int statuscode = response.getStatusLine().getStatusCode();
        // 7.擷取伺服器的返回實體
        HttpEntity entity = response.getEntity();

       //8.將伺服器返回的實體轉化成字串(請求響應結果)  
        String responseMsg = EntityUtils.toString(entity);
        System.out.println("介面:" + data.get("msgType") + ":返回的狀態代碼與響應結果:" + statuscode + ":" + responseMsg);
    }
}

使用HttpClient工具類測試WebService介面(Soap)與Http介面,這裡測試WebService介面,測試http介面實現方式類似

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.