使用HttpClient工具類測試Http接口

1、httpClient模擬客戶端java

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class GpoHttpClientServiceImpl {
    @DataProvider(name = "datapro")
    public Iterator<Object[]> datapro() {
        return new ExcelDataProvider("hospitalInv", "invokeGpoHttpServer");
    }

    @Test(dataProvider = "datapro")
    public void invokeGpoHttpServer(Map<String, String> data) throws Exception {

        // 建立http post請求
        HttpPost httppost = new HttpPost(data.get("httpUrl"));

        // 將請求的xml格式的字符串進行壓縮
        String zipReqxml = ZipUtil.zipBase64String(data.get("reqXml"));
        // 將請求的xml業務數據與接口密鑰一塊兒使用MD5加密,指定字符集爲UTF-8
        String sign = MD5Util.sign(data.get("reqXml"), data.get("mscpKey"), "utf-8");
        // 將服務端所須要的參數以BasicNameValuePair鍵值對的方式進行封裝,並添加到一個NameValuePair類型的list中,做爲傳送的容器,list中的每一個元素都是一個鍵值對
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("mscpCode", data.get("mscpCode")));
        params.add(new BasicNameValuePair("sign", sign));
        params.add(new BasicNameValuePair("reqXml", zipReqxml));
        // 設置http請求實體,http接口服務端接收數據的格式爲NameValuePair類型的list實體
        httppost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
        // 建立http客戶端
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 發送http post請求
        HttpResponse response = httpclient.execute(httppost);
        // 獲取服務端返回的httpCode
        int httpCode = response.getStatusLine().getStatusCode();
        // 將服務端返回的結果實體轉換成字符串
        String result = EntityUtils.toString(response.getEntity());
        System.out.println("HttpCode:" + httpCode + "   服務器返回結果:" + result);
    }

}

apache

 

2、MD5接口數據加密服務器

import java.io.UnsupportedEncodingException;
import java.security.SignatureException;
import org.apache.commons.codec.digest.DigestUtils;

public class MD5Util {

    /**
     * 簽名字符串
     *
     * @param text
     *            須要簽名的字符串
     * @param key
     *            密鑰
     * @param input_charset
     *            編碼格式
     * @return 簽名結果
     */
    public static String sign(String text, String key, String input_charset) {
        text = text + key;
        return DigestUtils.md5Hex(getContentBytes(text, input_charset));
    }

    /**
     * @param content
     * @param charset
     * @return
     * @throws SignatureException
     * @throws UnsupportedEncodingException
     */
    private static byte[] getContentBytes(String content, String charset) {
        if (charset == null || "".equals(charset)) {
            return content.getBytes();
        }

        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("MD5簽名過程當中出現錯誤,指定的編碼集不對,您目前指定的編碼集是:" + charset);
        }
    }
}ide

 

 

3、對傳輸的數據進行壓縮post

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.codec.binary.Base64;

public class ZipUtil {

    /**
     * 使用zip進行壓縮
     *
     * @param str
     *            壓縮前的文本
     * @return 返回壓縮後的文本
     */
    public static final String zipBase64String(String str) {
        if (str == null)
            return null;
        byte[] compressed;
        ByteArrayOutputStream out = null;
        ZipOutputStream zout = null;
        String compressedStr = null;
        try {
            out = new ByteArrayOutputStream();
            zout = new ZipOutputStream(out);
            zout.putNextEntry(new ZipEntry("zip"));
            zout.write(str.getBytes("utf-8"));
            zout.closeEntry();
            compressed = out.toByteArray();

            compressedStr = Base64.encodeBase64String(compressed);
        } catch (IOException e) {
            e.printStackTrace();
            compressed = null;
        } finally {
            if (zout != null) {
                try {
                    zout.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return compressedStr;
    }

    /**
     * 使用zip進行解壓縮
     *
     * @param compressed
     *            Base64轉碼的壓縮文本
     * @return 解壓後的字符串
     */
    public static String unzipBase642String(String base64CompressedStr) {
        if (base64CompressedStr == null) {
            return null;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = null;
        ZipInputStream ginzip = null;
        String decompressed = null;
        try {
            byte[] compressed = Base64.decodeBase64(base64CompressedStr);
            in = new ByteArrayInputStream(compressed);
            ginzip = new ZipInputStream(in);
            ginzip.getNextEntry();

            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString("utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
            if (ginzip != null) {
                try {
                    ginzip.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }

        return decompressed;
    }

}編碼

相關文章
相關標籤/搜索