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

1、需要导入httpclient包

    org.apache.httpcomponents
    httpclient
    4.5.3

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 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 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 params, Map 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 list = new ArrayList();
            for (Map.Entry 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 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 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 map = new HashMap();
    for(int i=0;i
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 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"}])

你可能感兴趣的:(Java 进阶 & Httpclient模拟从文件中获取的get、post请求)