httpClient工具类
来源:互联网 发布:volatile java 优缺点 编辑:程序博客网 时间:2024/05/22 11:16
httpclient工具类
本人封装的,常用的get、post表单提交、postJson提交都已做了封装,暂时没有加上请求超时时间
public class HttpClientUtil { /** * 发送get请求 * @param url * @return * @throws ClientProtocolException * @throws IOException * @throws URISyntaxException */ public static final String doGet(String url) throws ClientProtocolException, IOException, URISyntaxException{ return doGet(url,null); } /** * 发送get请求 * @param url * @param param * @return * @throws ClientProtocolException * @throws IOException * @throws URISyntaxException */ public static final String doGet(String url,Map<String,String> param) throws ClientProtocolException, IOException, URISyntaxException{ return doGet(url,null,null); } /** * 发送get请求 * @param url * @param param * @param headers * @return * @throws ClientProtocolException * @throws IOException * @throws URISyntaxException */ public static final String doGet(String url,Map<String,String> param,Map<String,Object> headers) throws ClientProtocolException, IOException, URISyntaxException{ CloseableHttpClient httpClient = HttpClients.createDefault(); String resultString =null; CloseableHttpResponse response =null; try{ URIBuilder uriBuilder = new URIBuilder(url); if(param!=null){ for(Map.Entry<String, String> entry:param.entrySet()){ uriBuilder.addParameter(entry.getKey(), entry.getValue()); } } HttpGet httpGet = new HttpGet(uriBuilder.build()); setHeaders(httpGet, headers); response=httpClient.execute(httpGet); if(response.getStatusLine().getStatusCode()==200){ resultString=EntityUtils.toString(response.getEntity(),"UTF-8"); } } finally{ if(response!=null){ response.close(); } httpClient.close(); } return resultString; } /** * 发送POST请求 * @param url * @param json * @return * @throws ClientProtocolException * @throws IOException */ public static final String doPostJson(String url,String json) throws ClientProtocolException, IOException{ return doPostJson(url, json,null); } /** * 发送POST请求 * @param url * @param json * @param headers * @return * @throws ClientProtocolException * @throws IOException */ public static final String doPostJson(String url,String json,Map<String,Object> headers) throws ClientProtocolException, IOException{ CloseableHttpClient httpClient = HttpClients.createDefault(); String resultString =null; CloseableHttpResponse response =null; try{ HttpPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON); httpPost.setEntity(stringEntity); httpPost.setHeader("Content-Type", "application/json"); setHeaders(httpPost, headers); response=httpClient.execute(httpPost); if(response.getStatusLine().getStatusCode()==200){ resultString=EntityUtils.toString(response.getEntity(),"UTF-8"); } } finally{ if(response!=null){ response.close(); } httpClient.close(); } return resultString; } /** * 以表单的方式提交post * @param url * @return * @throws ClientProtocolException * @throws IOException */ public static final String doPost(String url) throws ClientProtocolException, IOException{ return doPost(url,null); } /** * 以表单的方式提交post * @param url * @param param * @return * @throws ClientProtocolException * @throws IOException */ public static final String doPost(String url,Map<String,String> param) throws ClientProtocolException, IOException{ return doPost(url,param,null); } /** * 以表单的方式提交Post请求 * @param url * @param param * @param headers * @return * @throws ClientProtocolException * @throws IOException */ public static final String doPost(String url,Map<String,String> param,Map<String,Object> headers) throws ClientProtocolException, IOException{ CloseableHttpClient httpClient = HttpClients.createDefault(); String resultString =null; CloseableHttpResponse response =null; try{ HttpPost httpPost = new HttpPost(url); if(param!=null){ List<NameValuePair> nameValuePairs = new ArrayList<>(); for(Map.Entry<String, String> entry:param.entrySet()){ nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } UrlEncodedFormEntity urlEncodedFormEntity =new UrlEncodedFormEntity(nameValuePairs); httpPost.setEntity(urlEncodedFormEntity); } setHeaders(httpPost, headers); response=httpClient.execute(httpPost); if(response.getStatusLine().getStatusCode()==200){ resultString=EntityUtils.toString(response.getEntity(),"UTF-8"); } } finally{ if(response!=null){ response.close(); } httpClient.close(); } return resultString; } private static final void setHeaders(HttpUriRequest uriRequest,Map<String,Object> headers){ if(headers==null){ if(headers!=null){ for(Map.Entry<String, Object> entry:headers.entrySet()){ uriRequest.addHeader(entry.getKey(), entry.getValue().toString()); } } } }
阅读全文
0 0
- Android:HttpClient工具类
- HttpClient 操作工具类---
- httpclient工具类java
- httpclient工具类java
- HttpClient工具类
- HttpClient 操作工具类
- 自定义HttpClient工具类
- HttpClient联网工具类
- HttpClient 操作工具类
- HttpClient工具类
- httpClient工具类
- HttpClient工具类
- httpclient调用工具类
- HttpClient 工具类
- httpClient 工具类
- HttpClient的工具类
- java HttpClient 工具类
- Android 工具类httpClient
- JSON的用法与使用(1)
- Keras深度学习框架学习笔记系列(2)- Keras的安装与配置
- python练习(十七)
- python 网页显示 json序列化中文乱码
- myeclipse中类下面的图标形状含义
- httpClient工具类
- 由Dubbo回声测试学到的
- 使用Angularjs和Vue.js对比
- Vue2.0 新手入门 — 从环境搭建到发布
- 正则表达式学习笔记
- mysql5.7的root密码强度
- 音频位深度、采样率以及码率
- maven优化依赖的三个命令和eclipse中图像化界面的对应关系
- java大数BigInteger总结