HttpClient发送Get、Post请求简单实践

来源:互联网 发布:大数据培训骗局 编辑:程序博客网 时间:2024/05/17 23:34

1. 配置及实例化HttpClient:

private static final CloseableHttpClient httpclient;public static final String CHARSET = "UTF-8";static{RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(3000).build();httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();}

2. 发送Get请求方法:

/** * HTTP Get 获取内容 * @param url请求的url地址 ?之前的地址 * @param params请求的参数 * @param charset编码格式 * @return 页面内容 */public static String sendGet(String url, Map<String, Object> params) throws ParseException, UnsupportedEncodingException, IOException{if(params !=null && !params.isEmpty()){List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());for (String key :params.keySet()){pairs.add(new BasicNameValuePair(key, params.get(key).toString()));}url +="?"+EntityUtils.toString(new UrlEncodedFormEntity(pairs), CHARSET);}HttpGet httpGet = new HttpGet(url);CloseableHttpResponse response = httpclient.execute(httpGet);int statusCode = response.getStatusLine().getStatusCode();if(statusCode !=200){httpGet.abort();throw new RuntimeException("HttpClient,error status code :" + statusCode);}HttpEntity entity = response.getEntity();        String result = null;        if (entity != null) {            result = EntityUtils.toString(entity, "utf-8");            EntityUtils.consume(entity);            response.close();            return result;        }else{        return null;        }}

3. 发送Post请求方法:

/** * HTTP Post 获取内容 * @param url请求的url地址 ?之前的地址 * @param params请求的参数 * @param charset编码格式 * @return 页面内容 * @throws IOException  * @throws ClientProtocolException  */public static String sendPost(String url, Map<String, Object> params) throws ClientProtocolException, IOException {        List<NameValuePair> pairs = null;        if (params != null && !params.isEmpty()) {            pairs = new ArrayList<NameValuePair>(params.size());            for (String key : params.keySet()) {                pairs.add(new BasicNameValuePair(key, params.get(key).toString()));        }        }        HttpPost httpPost = new HttpPost(url);        if (pairs != null && pairs.size() > 0) {            httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));        }        CloseableHttpResponse response = httpclient.execute(httpPost);        int statusCode = response.getStatusLine().getStatusCode();        if (statusCode != 200) {            httpPost.abort();            throw new RuntimeException("HttpClient,error status code :" + statusCode);        }        HttpEntity entity = response.getEntity();        String result = null;        if (entity != null) {            result = EntityUtils.toString(entity, "utf-8");            EntityUtils.consume(entity);        response.close();        return result;        }else{         return null;        }}

4. 编写测试用例,进行Get 、Post请求方法的测试

4.1 测试Get方法,e .g:

@Testpublic void testGet() throws ParseException, UnsupportedEncodingException, IOException {String testUrl01 = "具体的测试接口地址";Map<String, Object> params01 = new HashMap<>();params01.put("参数01", "对应的参数取值");params01.put("参数02", "对应的参数取值");System.out.println(sendGet(testUrl01, params01));}

4.2 测试Post方法,e.g:

@Testpublic void testPost() throws ClientProtocolException, IOException {String testUrl02 = "具体的测试接口地址";Map<String, Object> params02 = new HashMap<>();params02.put("参数01", "对应的参数取值");params02.put("参数02", "对应的参数取值");System.out.println(sendPost(testUrl02, params02));}


最后

资料参考:http://blog.csdn.net/nevergiveuplzl/article/details/52304266

0 0
原创粉丝点击