天天看点

JAVA 发送POST,DELETE,PATCH,GET请求

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>      
package com.xh.sync;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.CharEncoding;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;


/**
 * @author H.Y
 * @date 2022/7/25
 */
@Slf4j
public class HttpUtils {

    private static final HttpClient httpClient = HttpClientBuilder.create().build();

    /**
     * 发送POST请求
     *
     * @param url
     * @param body
     * @return
     */
    public static String post(String url, String body) {
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-type", "application/json");

        try {
            httpPost.setEntity(new StringEntity(body));

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                log.debug("POST - url:{}, response:{}", url, response);
                return EntityUtils.toString(entity, CharEncoding.UTF_8);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发送DELETE请求
     *
     * @param url
     * @return
     */
    public static String delete(String url) {
        try {
            HttpResponse response = httpClient.execute(new HttpDelete(url));
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                log.debug("DELETE - url:{}, response:{}", url, response);
                return EntityUtils.toString(entity, CharEncoding.UTF_8);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发送PATCH请求
     *
     * @param url
     * @param body
     * @return
     */
    public static String patch(String url, String body) {
        HttpPatch httpPatch = new HttpPatch(url);
        httpPatch.setHeader("Content-type", "application/json");

        try {
            httpPatch.setEntity(new StringEntity(body));

            HttpResponse response = httpClient.execute(httpPatch);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                log.debug("PATCH - url:{}, response:{}", url, response);
                return EntityUtils.toString(entity, CharEncoding.UTF_8);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发送GET请求
     *
     * @param url
     * @return
     */
    public static String get(String url) {
        try {
            HttpResponse response = httpClient.execute(new HttpGet(url));
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                log.debug("GET - url:{},response:{}", url, response);
                return EntityUtils.toString(entity, CharEncoding.UTF_8);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发送PUT请求
     *
     * @param url
     * @param body
     * @return
     */
    public static String put(String url, String body) {
        HttpPut httpPut = new HttpPut(url);
        httpPut.setHeader("Content-type", "application/json");

        try {
            httpPut.setEntity(new StringEntity(body));

            HttpResponse response = httpClient.execute(httpPut);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                log.debug("put - url:{},response:{}", url, response);
                return EntityUtils.toString(entity, CharEncoding.UTF_8);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}