博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpClient封装工具类
阅读量:4568 次
发布时间:2019-06-08

本文共 3331 字,大约阅读时间需要 11 分钟。

import java.io.IOException;

import java.net.URI;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

 

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.client.utils.URIBuilder;

import org.apache.http.entity.ContentType;

import org.apache.http.entity.StringEntity;

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;

 

public class HttpClientUtil {

 

       public static String doGet(String url, Map<String, String> param) {

 

              // 创建Httpclient对象

              CloseableHttpClient httpclient = HttpClients.createDefault();

 

              String resultString = "";

              CloseableHttpResponse response = null;

              try {

                     // 创建uri

                     URIBuilder builder = new URIBuilder(url);

                     if (param != null) {

                            for (String key : param.keySet()) {

                                   builder.addParameter(key, param.get(key));

                            }

                     }

                     URI uri = builder.build();

 

                     // 创建http GET请求

                     HttpGet httpGet = new HttpGet(uri);

 

                     // 执行请求

                     response = httpclient.execute(httpGet);

                     // 判断返回状态是否为200

                     if (response.getStatusLine().getStatusCode() == 200) {

                            resultString = EntityUtils.toString(response.getEntity(), "UTF-8");

                     }

              } catch (Exception e) {

                     e.printStackTrace();

              } finally {

                     try {

                            if (response != null) {

                                   response.close();

                            }

                            httpclient.close();

                     } catch (IOException e) {

                            e.printStackTrace();

                     }

              }

              return resultString;

       }

 

       public static String doGet(String url) {

              return doGet(url, null);

       }

 

       public static String doPost(String url, Map<String, String> param) {

              // 创建Httpclient对象

              CloseableHttpClient httpClient = HttpClients.createDefault();

              CloseableHttpResponse response = null;

              String resultString = "";

              try {

                     // 创建Http Post请求

                     HttpPost httpPost = new HttpPost(url);

                     // 创建参数列表

                     if (param != null) {

                            List<NameValuePair> paramList = new ArrayList<>();

                            for (String key : param.keySet()) {

                                   paramList.add(new BasicNameValuePair(key, param.get(key)));

                            }

                            // 模拟表单

                            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);

                            httpPost.setEntity(entity);

                     }

                     // 执行http请求

                     response = httpClient.execute(httpPost);

                     resultString = EntityUtils.toString(response.getEntity(), "utf-8");

              } catch (Exception e) {

                     e.printStackTrace();

              } finally {

                     try {

                            response.close();

                     } catch (IOException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                     }

              }

 

              return resultString;

       }

 

       public static String doPost(String url) {

              return doPost(url, null);

       }

      

       public static String doPostJson(String url, String json) {

              // 创建Httpclient对象

              CloseableHttpClient httpClient = HttpClients.createDefault();

              CloseableHttpResponse response = null;

              String resultString = "";

              try {

                     // 创建Http Post请求

                     HttpPost httpPost = new HttpPost(url);

                     // 创建请求内容

                     StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);

                     httpPost.setEntity(entity);

                     // 执行http请求

                     response = httpClient.execute(httpPost);

                     resultString = EntityUtils.toString(response.getEntity(), "utf-8");

              } catch (Exception e) {

                     e.printStackTrace();

              } finally {

                     try {

                            response.close();

                     } catch (IOException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                     }

              }

 

              return resultString;

       }

}

转载于:https://www.cnblogs.com/zwjcom/p/6061799.html

你可能感兴趣的文章
python字典操作和内置方法
查看>>
【Windows】Windows Restart Manager 重启管理器
查看>>
vim切换编程语言_一步步将vim改造成C/C++开发环境(IDE) (转自:Figthing)
查看>>
cascade sqlite 数据库_SQLITE ON UPDATE操作
查看>>
python是动态数据类型语言_[Python basic]Python basic数据类型;强类型动态脚本语言,基础,基本...
查看>>
apache 代理 图片无法展示_Apache中间件漏洞详解
查看>>
android 底部上滑菜单_底部工作表
查看>>
linux查看显卡型号p4卡或者t4卡_装机宝典二十三式 | 为什么你直播那么卡?小老弟试试双卡推流吧...
查看>>
k均值聚类算法考试例题_K-means 聚类算法
查看>>
外卖匹配系统_浅谈搭建校园外卖配送平台的可行性分析
查看>>
android 代码设置居右_挖穿Android第四十九天
查看>>
命名时取代基优先顺序_浅谈有机化合物的英文命名(七)
查看>>
开启弹窗_弹窗广告总跳出来?学会这3种方法手机电脑再也不怕被打扰
查看>>
java_home没有定义_“错误:JAVA_HOME没有正确定义.”在构建Jikes rvm
查看>>
python canvas画移动物体_tkinter – 用于画布对象python的动画移动的方法
查看>>
java 连接 rac_JAVA 连接 ORACLE RAC 字符串
查看>>
java面试题 网络编程_java面试题《三、网络编程》
查看>>
java布尔矩阵程序_Java编程学习摘要(2)语法基础
查看>>
java no wait_即使队列在activemq中不为空,JMS实现中的receiveNoWait也返回null
查看>>
java定义player类_简易扑克牌游戏 定义了Constants、Main、Player、Poker四个类
查看>>