`
zysnba
  • 浏览: 179789 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

HttpClientUtil

 
阅读更多
第一步导入相对应的jar
   //httpclient
    compile 'commons-io:commons-io:2.6'
    compile 'org.jsoup:jsoup:1.11.3'
    compile 'org.apache.httpcomponents:httpclient:4.5'
    compile 'org.apache.httpcomponents:httpmime:4.5'
    compile 'org.apache.httpcomponents:httpcore:4.4.1'


第二步建立工具类

package com.www.util;

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;
    }

 
}
分享到:
评论

相关推荐

    HttpClientUtil工具类发送get和post请求,支持http和https,支持发送文件

    下面我们将详细讨论HttpClientUtil工具类如何实现这些功能。 首先,HttpClientUtil工具类通常会封装HttpClient的基本操作,以便于开发者在应用中便捷地调用。GET和POST请求是HTTP协议中最常见的两种请求方法。GET...

    httpClientUtil工具类

    httpClientUtil工具类

    http请求工具类HttpClientUtil,get,post请求(csdn)————程序.pdf

    1. **HttpClientUtil 类结构**: 类中定义了一个静态的日志器 LOGGER,用于记录执行过程中的信息,使用的是 SLF4J 日志框架。此外,创建了一个静态的 `CloseableHttpClient` 实例 `client`,这是 HttpClient 的核心...

    HttpClientUtil.java

    HttpClientUtil请求第三方接口,可以直接使用,post请求方式、get请求方式都有,直接粘贴复制就ok

    httpclientutil

    在"HttpClientUtil2-发送json返回json测试通过.java"这个文件中,我们可以预见到这是一个实现了发送JSON数据并接收JSON响应的示例。以下是一些关键知识点: 1. **Apache HttpClient 库**:HttpClient 是 Apache ...

    HttpClientUtil4.java

    HttpClientUtil4.java 工具类

    HttpClientUtil工具类,调用第三方接口

    该工具类是java 调用第三方接口时需要使用到的。HttpClientUtil 包含get和post方法。

    HttpClientUtil工具类 里面包含GET POST 及返回值处理

    - **实例化HttpClientUtil**:在代码中创建HttpClientUtil的实例。 - **调用方法**:根据需求,调用GET或POST方法,传入必要的参数(如URL、请求头、请求体等)。 - **处理返回值**:获取到的返回值通常是一个...

    httpclientUtil

    1.基于HttpClient-4.4.1封装的一个工具类; 2.基于HttpAsycClient-4.1封装的异步HttpClient工具类; 3.javanet包下面是基于jdk自带的UrlConnection进行封装的。 前2个工具类支持插件式配置Header、插件式配置...

    httpClientUtil

    自己做项目中用到的,以后保存下来以防以后用到。多多积累。

    HttpClientUtil2.java

    HttpClient汇总工具类 HttpClient汇总工具类HttpClient汇总工具类HttpClient汇总工具类

    基于jmeter+Java+HttpclientUtil实现的接口测试工具

    基于jmeter+Java+HttpclientUtil实现的接口测试工具,通过Excel表格进行维护接口相关参数信息,借助Jmeter工具通过java请求设计接口测试自动化测试用例。代码重写了JavaSampleClient类,在使用过程中取出了冗余的...

Global site tag (gtag.js) - Google Analytics