httpclient4.3发送post和get实例代码
来源:互联网 发布:软件前景 编辑:程序博客网 时间:2024/06/05 16:30
httpclient4.3发送post和get请求的工具代码。
/** * @throws IOException * * @Title: httpPost * @Description: httpPost请求工具类 * @param url * @param params * @param charset * @param socketTimeOut * @param connectTimeOut * @param connReqTimeOut * @return 设定文件 * @return String 返回类型 * @date 2015年7月10日 上午11:55:39 * @throws */ public static String httpPost(String url, Map<String, Object> params, String charset, int socketTimeOut, int connectTimeOut, int connReqTimeOut ) throws IOException{ if (Tools.checkNull(charset)) { charset = UTF8; } HttpPost httppost = new HttpPost(url); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeOut).setConnectTimeout(connectTimeOut).setConnectionRequestTimeout(connReqTimeOut) .build();//设置请求和传输超时时间 httppost.setConfig(requestConfig); //定义传递参数 List<NameValuePair> formparams = new ArrayList<NameValuePair>(); if (params != null) { for (Entry<String,Object> entry : params.entrySet()) { formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()==null?"":entry.getValue().toString())); } } UrlEncodedFormEntity uefEntity; String result = null; CloseableHttpClient httpclient = HttpClients.custom().build(); CloseableHttpResponse response = null; try{ uefEntity = new UrlEncodedFormEntity(formparams, charset);//对参数进行编码 httppost.setEntity(uefEntity); response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toString(entity, charset); } } else { return result; } } finally{ if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return result; } /** * @throws IOException * * @Title: httpPost * @Description: httpPost请求工具类 * @param url * @param params * @param timeOut * @param charset * @return 设定文件 * @return String 返回类型 * @date 2015年7月2日 下午4:51:55 * @throws */ public static String httpPost(String url, Map<String, Object> params, String charset, int timeOut) throws IOException{ return httpPost(url, params, charset, timeOut, timeOut, timeOut); } /** * @throws IOException * * @Title: httpGet * @Description: httpPost请求工具类 * @param url * @param params * @param charset * @param socketTimeOut * @param connectTimeOut * @param connReqTimeOut * @return 设定文件 * @return String 返回类型 * @date 2015年7月10日 上午11:55:39 * @throws */ public static String httpGet(String url, Map<String, Object> params, String charset, int socketTimeOut, int connectTimeOut, int connReqTimeOut ) throws IOException{ if (Tools.checkNull(charset)) { charset = UTF8; } log.info("url : [{}]", url); String result = null; CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = null; try{ //定义传递参数 List<NameValuePair> formparams = new ArrayList<NameValuePair>(); for (Entry<String,Object> entry : params.entrySet()) { formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()==null?"":entry.getValue().toString())); } url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(formparams), charset); HttpGet httpget = new HttpGet(url); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeOut).setConnectTimeout(connectTimeOut).setConnectionRequestTimeout(connReqTimeOut) .build();//设置请求和传输超时时间 httpget.setConfig(requestConfig); response = httpclient.execute(httpget); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toString(entity, charset); } } else { return result; } } finally{ if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return result; } /** * @throws IOException * * @Title: httpGet * @Description: httpGet请求工具类 * @param url * @param params * @param charset * @param timeOut * @return 设定文件 * @return String 返回类型 * @date 2015年7月15日 上午10:34:41 * @throws */ public static String httpGet(String url, Map<String, Object> params, String charset, int timeOut) throws IOException{ return httpGet(url, params, charset, timeOut, timeOut, timeOut); }
httpclient4.3 jar包
1 0
- httpclient4.3发送post和get实例代码
- httpclient4 发送http请求的get和post用法
- Java使用HttpClient4.3发送GET/POST请求
- HttpClient4.x:Get和Post提交数据
- httpclient4.5的get和post方法
- 用httpclient4.x 发送http get post请求。
- 用httpclient4.x 发送http get post请求。
- 使用Apache HttpClient4.x 发送 GET POST 请求
- 关于HttpClient4.3.*的Post和Get提交乱码问题
- java 利用httpclient 3.1 和 httpclient4.1.2发送post请求
- java 利用httpclient 3.1 和 httpclient4.1.2发送post请求
- java 利用httpclient 3.1 和 httpclient4.1.2发送post请求
- HttpClient4.5发送post请求
- HttpClient4.3 post与get请求工具类完整示例
- java代码 发送GET、POST请求
- HttpClient4.5 get访问实例
- LoadRunner总结(3)-LoadRunner发送get和post请求
- get和post模拟发送和数据处理
- hue安装运行时候提示:oozie share lib not installed in default location.
- Jquery下拉框取值
- 【转】iOS UIButton(UIEdgeInsets)
- Android学习笔记 3.3View的弹性滑动
- 棋盘分割
- httpclient4.3发送post和get实例代码
- html中META标签的作用及SEO关键字优化办法
- Elasticsearch源码分析九--查询解析器QueryParser注册过程
- Jquery 多选下拉框取值
- 基于HTK的连续语音识别系统搭建学习笔记(一)
- Oracle 增加修改删除字段
- 趣学 C 语言 (一)—— time.h
- 详解Oracle DELETE和TRUNCATE 的区别
- Spring的PropertyPlaceholderConfigurer应用