首页 > 编程语言 >java http请求工具类

java http请求工具类

时间:2022-12-07 09:25:00浏览次数:48  
标签:java 请求 url params result new http response String

1、首先maven

<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.5</version>
</dependency>

2、创建HttpClientUtils

public class HttpClientUtils {

    public static String doGet(String url) {
        return doGet(url, null);
    }
    public static String doGet(String url, Map<String, String> params) {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String result = "";
        CloseableHttpResponse response = null;
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (params != null) {
                for (String key : params.keySet()) {
                    builder.addParameter(key, params.get(key));
                }
            }
            URI uri = builder.build();
            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                result = 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 result;
    }
    public static String doPost(String url) {
        return doPost(url, null);
    }
    public static String doPost(String url, Map<String, String> params) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建参数列表
            if (params != null) {
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : params.keySet()) {
                    paramList.add(new BasicNameValuePair(key, params.get(key)));
                }
                // 模拟表单
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8");
                httpPost.setEntity(entity);
            }
            // 执行http请求
            response = httpClient.execute(httpPost);
            result = 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 result;
    }
    public static String doPostJson(String url, String json) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建请求内容
            StringEntity entity = new StringEntity(json.toString(),"UTF-8");//解决中文乱码问题
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(httpPost);
            result = 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 result;
    }
}

标签:java,请求,url,params,result,new,http,response,String
From: https://www.cnblogs.com/h-w-b/p/16962078.html

相关文章

  • java 身份证号校验
    创建CardUtil工具类publicclassCardUtil{/***验证身份证真假*@paramcarNumber身份证号*@returnboolean*/publicstaticboolean......
  • 1.5.4 HDFS 客户端操作-hadoop-最全最完整的保姆级的java大数据学习资料
    目录1.5.4HDFS客户端操作1.5.4.1Shell命令行操作HDFS1.5.4.2JAVA客户端1.5.4.2.1客户端环境准备1.5.4.2.2HDFS的API操作1.5.4.2.2.1上传文件1.5.4.2.2.2下载文件1......
  • Java web 概念
          ......
  • JavaSE复习day2
    JavaSE复习day2胡家伟7.数组定义数组是一种在内存中存储数据的载体,相比普通变量,同时拥有多个空间。基本数据类型的值直接存储在变量所声明的对应的栈空间中,引用数据......
  • Java入门(二) —— 开发工具配置
    教程中所用到的工具请关注微信公众号:科技前端,回复“工具”即可获得。Java开发工具最常用的就是Eclipse和IDEA,不过我个人推荐用IDEA。下面我将分别介绍这两个软件。一、E......
  • java Collection 排序
    Integer排序Collections.sort(resList,Comparator.comparingInt(SpecialStateCountVo::getSpecialNum).reversed());String排序list=list.stream().sorted(Comparat......
  • 在Maven中出现javax.mail中文乱码问题解决记录
    学习Java时,看廖雪峰大神文章使用了javax.mail来发送SMTP邮件。在加入中文时,发现收到的邮件里中文都是乱码。按照一般经验,多半是编码的问题。然而在代码中,会涉及到编码的地......
  • Java学习(基础)
    学习视频:Java零基础学习视频通俗易懂Java注释单行注释//多行注释/**/文档注释:/****/有趣的代码注释/****_ooOoo_*......
  • 当我们的执行 java -jar xxx.jar 的时候底层到底做了什么?
    大家都知道我们常用的SpringBoot项目最终在线上运行的时候都是通过启动java-jarxxx.jar命令来运行的。那你有没有想过一个问题,那就是当我们执行java-jar命令后,到......
  • Java入门学习
    P13Java帝国的诞生Java初生:Java2标准版(J2SE):去占领桌面Java2移动版(J2ME):去占领手机Java2企业版(J2EE):去占领服务器Java发展:构建工具:Ant,Maven,Jekins应用服......