java——HttpClient's GET and POST request instance tool

A brief introduction to HTTP:

The HTTP protocol may be the most widely used and most important protocol on the Internet now, and more and more Java applications need to directly access network resources through the HTTP protocol. Although the basic functions of accessing the HTTP protocol have been provided in the java.net package of the JDK, for most applications, the functions provided by the JDK library itself are not rich and flexible enough. HttpClient is a sub-project under Apache Jakarta Common, which is used to provide an efficient, up-to-date, feature-rich client programming toolkit supporting the HTTP protocol, and it supports the latest version and recommendations of the HTTP protocol.

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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StringUtils;

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

/**
 * @Author :feiyang
 * @Date :Created in 2019/5/15
 */
public class HttpClientUtil {

    private static CloseableHttpClient httpClient = HttpClients.createDefault();

    /**
     * POST 有参
     * @param host
     * @param port
     * @param uri
     * @param paramsMap
     * @return
     */
    public static String sendPost(String host, String port, String uri, Map<String, Object> paramsMap){
        try {
            String url = "http://"+host+":"+port+"/"+uri;
            HttpPost post = new HttpPost(url);
            if (!StringUtils.isEmpty(paramsMap)){
                List<NameValuePair> params = new ArrayList<>();
                for (Map.Entry<String, Object> entry : paramsMap.entrySet()
                ) {
                    params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
                }
                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8");
                post.setEntity(formEntity);
            }
            CloseableHttpResponse  response = httpClient.execute(post);
            if (!StringUtils.isEmpty(response.getEntity())){
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * POST 无参
     * @param host
     * @param port
     * @param uri
     * @return
     */
    public static String sendPost(String host, String port, String uri){
        try {
            String url = "http://"+host+":"+port+"/"+uri;
            HttpPost post = new HttpPost(url);
            CloseableHttpResponse response = httpClient.execute(post);
            if (!StringUtils.isEmpty(response.getEntity())){
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * GET 有参
     * @param host
     * @param port
     * @param uri
     * @param paramsMap
     * @return
     */
    public static String sendGet(String host, String port, String uri, Map<String, Object> paramsMap){
        try {
            String url = "http://"+host+":"+port+"/"+uri;
            URIBuilder uriBuilder = new URIBuilder(url);
            if (paramsMap != null) {
                for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
                    uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
                }
            }
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            CloseableHttpResponse response = httpClient.execute(httpGet);
            if (response.getEntity() != null) {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * GET 无参
     * @param host
     * @param port
     * @param uri
     * @return
     */
    public static String sendGet(String host, String port, String uri){
        try {
            String url = "http://"+host+":"+port+"/"+uri;
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse response = httpClient.execute(httpGet);
            if (response.getEntity() != null) {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

 

Guess you like

Origin blog.csdn.net/qq_42133100/article/details/90242757