HttpClient POST/SET方法

前言:

网络API接口:https://api.apiopen.top/searchMusic

此API接口返回类型为JSON格式类型

GET:从指定资源请求数据

POST:向指定资源提交要被处理的数据

GET与POST的区别:

①GET在浏览器回退时是无害的,而POST会再次提交请求。

②GET只支持URL编码。

③GET参数通过URL传递,参数直接暴露在URL中会泄露信息,POST通过Request body传递不会有这样的问题。

④GET请求在URL中传递参数有长度限制,POST没有长度限制。

⑤对POST请求会险发送Header,服务器相应成功,在发送Date,服务器再次响应,需响应两次。GET会把Header与Date一起发送到服务端,服务器只响应一次。

一、POST方法

创建POST方法类

package com.HttpClient.Test;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.HttpClient.jiexi.HttpClient_jiexi;

public class HttpClient_post {
    
    private String entityStr;
    HttpClient_jiexi JSONTOOL = new HttpClient_jiexi();
    
    //封装POST方法
    public String post(String POST_URL,ArrayList<NameValuePair> list) {
        
        try {
            //把参数放入请求体中
            UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost(POST_URL);
            httpPost.setEntity(entityParam);
            //发起请求
            CloseableHttpResponse response = httpClient.execute(httpPost);
            //获取返回状态,并判断是否连接成功。
            if (response.getStatusLine().getStatusCode()==200) {
                System.out.println("连接成功");
            } else {
                System.out.println("连接异常");
            }
            // 获得响应的实体对象
            HttpEntity entity = response.getEntity();
            
            //转换成字符串
            entityStr = EntityUtils.toString(entity, "UTF-8");
            //关闭请求
            httpClient.close();
            
        } catch (ClientProtocolException e) {
            System.err.println("Http协议异常");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IO异常");
            e.printStackTrace();
        }
        return entityStr;
    }

}

调用POST方法类

package com.HttpClient.Test;

import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.testng.annotations.Test;

public class HttpClient_case1 {
    
    HttpClient_post post = new HttpClient_post();
    private String POST_URL = "https://api.apiopen.top/searchMusic";
    private String entity;
    private ArrayList<NameValuePair> list;
    
    @Test
    public void test1() {   
            // 构造list集合,并添加参数
            BasicNameValuePair basicNameValuePair = new BasicNameValuePair("name", "十年");
            list = new ArrayList<>();
            list.add(basicNameValuePair);
            // 执行请求,需要传入URL与参数
            entity = post.post(POST_URL,list);
            System.out.println(entity);
    }

}

二、SET方法

创建SET方法类

package com.HttpClient.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpClient_set {
    
    public String entityStr ;
    public CloseableHttpResponse response ;

    
    public String set(String GET_URL,ArrayList<NameValuePair> list) throws URISyntaxException{try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            URIBuilder uriBuilder = new URIBuilder(GET_URL);
            uriBuilder.setParameters(list);
            //根据带参数的URI对象构建GET请求对象
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            //执行请求
            response = httpClient.execute(httpGet);
            //获得响应的实体对象
            HttpEntity entity = response.getEntity();
            //转换成字符串
            entityStr = EntityUtils.toString(entity, "UTF-8");

            //关闭请求
            httpClient.close();
        } catch (ClientProtocolException e) {
            System.err.println("Http协议异常");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IO异常");
            e.printStackTrace();
        }
        return entityStr;
        
    }

}

调用SET方法类

package com.HttpClient.Test;

import java.net.URISyntaxException;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.testng.annotations.Test;

public class HttpClient_case1 {
    
    HttpClient_set set = new HttpClient_set();
    private String SET_URL = "https://api.apiopen.top/searchMusic";
    private String entity;
    private ArrayList<NameValuePair> list;
    
    @Test
    public void test1() throws URISyntaxException {   
            // 构造list集合
            BasicNameValuePair basicNameValuePair = new BasicNameValuePair("name", "十年");
            list = new ArrayList<>();
            list.add(basicNameValuePair);
            // 执行请求,需要传入URL与参数
            entity = set.set(SET_URL, list);
            System.out.println(entity);
            
    }

}

相关推荐