基于httpclient的get和post工具类
来源:互联网 发布:零基础学c语言 知乎 编辑:程序博客网 时间:2024/06/04 18:53
httpclient的maven地址:
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version></dependency>
HttpRequestUtils类:
import net.sf.json.JSONObject;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;/** * HTTP请求工具类 * @author dreamof2015 */public class HttpRequestUtils { /** * post请求 * @param url * @param jsonParam * @return */ public static JSONObject httpPost(String url, JSONObject jsonParam){ CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost post = new HttpPost(url); JSONObject jsonObject = new JSONObject(); if(jsonParam!=null){ Iterator<String> sIterator = jsonParam.keys(); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); while(sIterator.hasNext()){ String key = sIterator.next(); String value = jsonParam.getString(key); nvps.add(new BasicNameValuePair(key,value)); } try { post.setEntity(new UrlEncodedFormEntity(nvps)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } try { CloseableHttpResponse response = httpclient.execute(post); HttpEntity entity = response.getEntity(); jsonObject.put("status",response.getStatusLine()); EntityUtils.consume(entity); jsonObject.put("responseText",entity.getContent()); } catch (IOException e) { e.printStackTrace(); } return jsonObject; } /** * get请求 * @param url * @return */ public static JSONObject httpGet(String url){ CloseableHttpClient httpclient = HttpClients.createDefault(); JSONObject jsonObject = new JSONObject(); HttpGet httpGet = new HttpGet(url); try { CloseableHttpResponse response = httpclient.execute(httpGet); jsonObject.put("status",response.getStatusLine()); HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); jsonObject.put("responseText",entity.getContent()); } catch (IOException e) { e.printStackTrace(); } return jsonObject; }}
阅读全文
0 0
- 基于httpclient的get和post工具类
- HttpClient的Post和Get各种方法的工具类
- HttpClient发送get和post工具类
- HttpClient 的Get和Post
- HttpClient的get和post
- httpclient 4.3.1 post get的工具类
- httpclient 4.3.1 post get的工具类
- HttpClient 的get post传值工具类
- httpclient 4.3.1 post get的工具类
- httpclient 4.3.2 post get的工具类
- 用HttpClient发送get,post请求的工具类
- Apache 大三方法HTTP请求链接响应之HttpClient的GET和POST工具类封装
- httpclient工具类 get post(4.3.5)
- httpclient的post和get,httpurlconnection的post和get
- HttpClient Get和Post
- httpclient的get和post请求
- Post和Get在HttpClient的使用
- HttpClient的Post和Get访问网页
- 数据结构单链表的定义(Java)
- getTime,localtime,mktime
- 基于 DCT 过完备字典和 K-SVD 的图像稀疏去噪方法
- 计算机视觉、机器学习相关领域论文和源代码大集合
- Linux内核实验要注意的地方
- 基于httpclient的get和post工具类
- js手机验证码倒数计时
- 从1到0思考架构
- 多线程交替打印数字
- 正则表达式验证
- 查找代理工具,发现一篇火星文
- oracle 触发器demo
- SpringMVC开发包下载地址
- Define函数- JavaScript