一、httpClient模拟客户端

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

}

二、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);
        }
    }
}

三、对传输的数据进行压缩

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;
    }

}

使用HttpClient工具类测试Http接口的更多相关文章

  1. 使用HttpClient工具类测试WebService接口(soap)

    import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import jav ...

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

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

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

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

  4. 基于HttpClient4.5.2实现的HttpClient工具类

    1.maven依赖: <dependency> <groupId>org.apache.commons</groupId> <artifactId>co ...

  5. 转:轻松把玩HttpClient之封装HttpClient工具类(一)(现有网上分享中的最强大的工具类)

    搜了一下网络上别人封装的HttpClient,大部分特别简单,有一些看起来比较高级,但是用起来都不怎么好用.调用关系不清楚,结构有点混乱.所以也就萌生了自己封装HttpClient工具类的想法.要做就 ...

  6. mybatis的基本配置:实体类、配置文件、映射文件、工具类 、mapper接口

    搭建项目 一:lib(关于框架的jar包和数据库驱动的jar包) 1,第一步:先把mybatis的核心类库放进lib里

  7. 使用单例模式实现自己的HttpClient工具类

    引子 在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient 来方便我们使用各种Http服务.你可以把HttpCli ...

  8. Android开发实现HttpClient工具类

    在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务.你可以把HttpClient想 ...

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

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

随机推荐

  1. Struts2的DMI跟SMI

    我使用的Struts2的版本是2.5.2,今天在使用Struts2的DMI(动态方法调用)的时候出现了一个有趣的问题,我先把我的配置及代码展示一下: web.xml <filter> &l ...

  2. Hibernate 框架基本知识

    QTP:Quick Test Pressional 1,Hibernate是一个优秀的java持久化层解决方案,是当今主流的对象-关系映射(ORM,ObjectRelationalMapping)工具 ...

  3. eclipse 启动tomcat后 页面无法访问tomcat首页

    在eclipse中新建tomcat7,完成后tomcat能够正常启动,但是浏览器问题localhost:8080访问不了. 解决方法如下: 双击eclipse中服务器中的tomcat 出现tomcat ...

  4. tomcat设置http自动跳转为https访问

    一.生成服务器端证书文件 可以使用Windows系统或者Linux系统 (1)Windows环境 条件:已经安装JDK 步骤: 1.在运行里输入cmd进入命令窗口 2.进入JDK安装目录  如D:/P ...

  5. Java之JSP基础语法

    1.JSP页面元素简介及page指令     2.JSP注释,3种不同注释 <!--  我是HTML注释,在客户端可见 --> <%--我是JSP注释,在客户端不可见 --%> ...

  6. 更新BLUZ

    bluez的编译安装依赖好些软件,下面记录下,可能比较简陋. configure: error: GLib >= 2.28 is required解决方法:一般glib会被安装,主要是一些开发文 ...

  7. SQL Server 2008新特性——更改跟踪

    在大型的数据库应用中,经常会遇到部分数据的脱机和多个数据库的合并问题.比如现在有一个全省范围使用的应用程序,每个市都部署了单独的相同的应用程序服务器和数据库服务器,每个月需要将全省所有市的数据全部汇总 ...

  8. Eclipse 打开当前文件所在的文件夹

    两种方法: 1.安装EasyExplorer插件,有了这个插件就可以很方便地打开资源文件所在的文件夹了. EasyExplorer 从 http://sourceforge.net/projects/ ...

  9. vs2008编译wxWidgets 2.8.12

    用vs2008编译wxWidgets 2.8.12 FileZilla客户端源码3.5.3及以上版本依赖wxWidgets 2.8.12或更高版本,因此,编译FileZilla客户端首先需要编译wxW ...

  10. 组合,关联,聚合的区别(转自http://jimmyleeee.blog.163.com/blog/static/9309618200932014422932/)

    类间关系 在类图中,除了需要描述单独的类的名称.属性和操作外,我们还需要描述类之间的联系,因为没有类是单独存在的,它们通常需要和别的类协作,创造比单独工作更大的语义.在UML类图中,关系用类框之间的连 ...