JAVA [Basic] Use HttpClient to send get and post requests

The httpclient package is often used in recent projects. Here is a brief package for everyone to use.
Not much nonsense, just go to the code.


Before the introduction of httpclient, maven depends on the following

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>

Remember to import the log4j package, because httpclient uses log4j to output

<dependency>
	<groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
	<version>1.2.17</version>
</dependency>

Tools are as follows

method Description
String post(String url,List<NameValuePair> list) Perform a post request, pass in the list collection data, and use it BasicNameValuePairas a collection element. Return response data
String post(String url,BasicNameValuePair...pairs) Execute post request, you can pass in more than one BasicNameValuePair, or leave it blank. Return response data
String get(String url) Execute get request and return response data
/**
 * @Author: KL-Skeleton
 * @Description:
 * @Date: Created in 18:47 2020/7/30
 */

public class HttpUtil {
    
    

    //post请求,带集合参数
    public static String post(String url,List<NameValuePair> list) throws Exception {
    
    
        // 通过HttpPost来发送post请求
        HttpPost httpPost = new HttpPost(url);
        // 我们发现Entity是一个接口,所以只能找实现类,发现实现类又需要一个集合,集合的泛型是NameValuePair类型
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list);
        // 通过setEntity 将我们的entity对象传递过去
        httpPost.setEntity(formEntity);
        return execute(httpPost);
    }

    //post请求,不带参数,或者带多个参数
    public static String post(String url,BasicNameValuePair...pairs) throws Exception {
    
    
        List<NameValuePair> pairs1 = new ArrayList<>();
        for(BasicNameValuePair nameValuePair: pairs){
    
    
            pairs1.add(nameValuePair);
        }
        return  post(url,pairs1);
    }

    //get请求
    public static String  get(String url) throws Exception {
    
    
        return execute(new HttpGet(url));
    }

    //执行请求返回响应数据
    public static String execute(HttpRequestBase http)  throws Exception {
    
    

        CloseableHttpClient client = HttpClients.createDefault();
        // 通过client调用execute方法
        CloseableHttpResponse Response = client.execute(http);
        //获取响应数据
        HttpEntity entity = Response.getEntity();
        //将数据转换成字符串
        String str = EntityUtils.toString(entity, "UTF-8");
        //关闭
        Response.close();
        return  str;
    }
}

The test is as follows

        try {
    
    
            //get请求
            System.out.println(HttpUtil.get("https://www.baidu.com/"));
            
            //post请求,不带参数
            System.out.println(HttpUtil.post("https://www.baidu.com/"));
            
            //post请求,带一个参数 ,类似 https://www.baidu.com/?user=skeleton
            System.out.println(HttpUtil.post("https://www.baidu.com/",new BasicNameValuePair("user","skeleton")));
           
           //post请求,带多个参数 ,类似 https://www.baidu.com/?user=skeleton&pwd=123456
            List<NameValuePair> pairs = new ArrayList<>();
            pairs.add(new BasicNameValuePair("user","skeleton"));
            pairs.add(new BasicNameValuePair("pwd","123456"));
            System.out.println(HttpUtil.post("https://www.baidu.com/",pairs));

        } catch (Exception e) {
    
    
            e.printStackTrace();
        }

Insert picture description here
If you want to remove the debugging information of httpclient, you can add it to the log4j configuration file:

###将httpclient输出日志级别调到ERROR
log4j.logger.org.apache.http = error
log4j.logger.httpclient.wire = error

The result is as follows
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_31254489/article/details/108003236