Java 进阶 & Httpclient模拟从文件中获取的get、post请求

1、需要导入httpclient包
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>
2、新建Data.txt
image.png
3、Httpclient模拟get请求
private static CloseableHttpClient httpclient;
static {
    PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
    manager.setMaxTotal(200); //连接池最大并发连接数
    manager.setDefaultMaxPerRoute(200);//单路由最大并发数,路由是对maxTotal的细分
    httpclient = HttpClients.custom().setConnectionManager(manager).build();
}

/* ConnectionRequestTimeout httpclient使用连接池来管理连接,这个时间就是从连接池获取连接的超时时间,可以想象下数据库连接池
   ConnectTimeout 建立连接最大时间
   SocketTimeout 数据传输过程中数据包之间间隔的最大时间
   HttpHost 代理
 */
private static RequestConfig config =RequestConfig.copy(RequestConfig.DEFAULT)
        .setSocketTimeout(10000)
        .setConnectTimeout(5000)
        .setConnectionRequestTimeout(100).build();
       // .setProxy(new HttpHost("127.0.0.1",8888,"http")).build();

public static String doGet(String url, Map<String, Object> header)
        throws HttpClientException {
    String ret = "";
    HttpGet get = new HttpGet(url);
    get.setConfig(config);
    get.addHeader(HTTP.CONTENT_ENCODING, "UTF-8");
    CloseableHttpResponse closeableHttpResponse = null;
    try {
        if (header != null) {
            for (Map.Entry<String, Object> entry : header.entrySet()) {
                get.setHeader(entry.getKey(), entry.getValue().toString());
            }
        }
        closeableHttpResponse = httpclient.execute(get);
        if (closeableHttpResponse.getStatusLine().getStatusCode() == 200) {
            ret = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
        } else {
            throw new HttpClientException(
                    "System level error, Code=[" + closeableHttpResponse.getStatusLine().getStatusCode() + "].");
        }
    } catch (ClientProtocolException e) {
        throw new HttpClientException("HttpClient error," + e.getMessage());
    } catch (IOException e) {
        throw new HttpClientException("IO error," + e.getMessage());
    } finally {
        if (closeableHttpResponse != null) {
            try {
                closeableHttpResponse.close();
            } catch (IOException e) {
            }
        }
    }
    return ret;
}
public static String doGet(String url) throws HttpClientException {
    return doGet(url,null);
}
4、Httpclient模拟post请求
public static String doPost(String url, Map<String, Object> params, Map<String, Object> header)
        throws HttpClientException {
    String ret = "";
    HttpPost post = new HttpPost(url);
    post.setConfig(config);
    post.addHeader(HTTP.CONTENT_ENCODING, "UTF-8");
    CloseableHttpResponse closeableHttpResponse = null;
    HttpEntity postEntity = null;
    try {
        if(params!=null) {
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                list.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
            postEntity = new UrlEncodedFormEntity(list);
            post.setEntity(postEntity);
        }

        if (header != null) {
            for (Map.Entry<String, Object> entry : header.entrySet()) {
                post.setHeader(entry.getKey(), entry.getValue().toString());
            }
        }
        closeableHttpResponse = httpclient.execute(post);
        if (closeableHttpResponse.getStatusLine().getStatusCode() == 200) {
            ret = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
        } else {
            throw new HttpClientException(
                    "System level error, Code=[" + closeableHttpResponse.getStatusLine().getStatusCode() + "].");
        }
    } catch (ClientProtocolException e) {
        throw new HttpClientException("HttpClient error," + e.getMessage());
    } catch (IOException e) {
        throw new HttpClientException("IO error," + e.getMessage());
    } finally {
        if(postEntity!=null) {
            try {
                EntityUtils.consume(postEntity);
            } catch (IOException e) {
            }
        }
        if (closeableHttpResponse != null) {
            try {
                closeableHttpResponse.close();
            } catch (IOException e) {
            }
        }
    }
    return ret;
}
public static String doPost(String url, Map<String, Object> params) throws HttpClientException {
    return doPost(url,params,null);
}
5、将字符串转化成Map,重载doPost方法:
public static String doPost(String url, String params,String regex) throws HttpClientException {
    String[] param =params.split(regex);
    Map<String,Object> map = new HashMap<String,Object>();
    for(int i=0;i<param.length;i++){
        String[] pp = param[i].split("=");
        for(String p:pp){
            map.put(pp[0],pp[1]);
        }
    }
    return doPost(url,map,null);
}
6、创建测试类
public static void main(String[] args) throws HttpClientException, IOException {
    String filePath = System.getProperty("user.dir") + File.separator + "testdata" + File.separator;
    File file = new File(filePath + "data.txt");
    File file1 = new File(filePath + "result.txt");
    String ret = "";
    List<String> lines = FileUtils.readLines(file, "UTF-8");
    for (int m = 0; m < lines.size(); m++) {
        if (m > 0) {
            String[] ss = lines.get(m).split(";");
            String url = ss[0];
            String method = ss[1];
            if ("get".equals(method)) {
                ret = HttpUtils.doGet(url);
                writeTofile(file1, "\nget ------>" + ret);
                System.out.println("get------>" + ret);
            } else if ("post".equals(method)) {
                ret = HttpUtils.doPost(url, ss[2], "&");
                writeTofile(file1, "\npost ------>" + ret);
                System.out.println("post------>" + ret);
            }
        }
    }
}
public static void writeTofile(File file,String str) throws IOException {
    FileUtils.writeStringToFile(file,str,"utf-8",true);

}
7、查看输出结果
get------>DataFunc([{"TID":"8519","retCode":"1"}])
post------>DataFunc([{"TID":"8519","retCode":"1"}])
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、背景介绍 在日常的接口测试工作中,模拟接口请求通常有两种方法,fiddler模拟和HttpClient模拟。 ...
    隋胖胖LoveFat阅读 14,762评论 3 39
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,293评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,107评论 19 139
  • 这是一部我早已听说却迟迟没有打开的书,说实话,我对东野圭吾并无多少好奇,因为在我的印象里他的作品大多是关于犯罪的,...
    秋千上的老狗阅读 1,603评论 0 0

友情链接更多精彩内容