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
- HttpClient发送Get、Post请求简单实践
- java httpclient发送post get 请求
- HttpClient发送get和post请求
- 使用Apache HttpClient发送get、post请求
- 利用HttpClient发送Get和Post请求
- 利用HttpClient发送Get和Post请求
- 利用HttpClient发送Get和Post请求
- 利用HttpClient发送Get和Post请求
- HttpClient发送Get和Post请求
- 使用HttpClient发送GET和POST请求
- 使用httpclient发送get/post请求
- 使用httpclient发送get或post请求
- httpurlconnection 和 httpclient 发送get、post请求
- HttpClient发送post请求,和发送get请求
- Java使用HttpClient发送Get请求和Post请求
- httpClient post/get请求。
- HttpClient get,Post请求
- HttpClient get、post请求
- PHP 取302跳转后真实 URL 的两种方法
- HPUOJ1163: 大数A + B [字符串](水)
- 1101. Quick Sort (25)
- phxpaxos learn模块解析
- 浅谈java中udp服务器客户端实例
- HttpClient发送Get、Post请求简单实践
- Ubuntu 14.04 Web 程序开发(4)基于JQuery+Ajax+Json+Servlet实现PUT GET
- 原创:王者皮肤修改器--恶意代码分析手记
- Java 泛型
- jQuery中的Ajax
- DevExpress.XtraTreeList.TreeList筛选DevExpress.XtraGrid.Views.Grid.GridView筛选
- Java 序列化
- C/C++经典程序训练2---斐波那契数列
- Java 网络编程