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

本文详细介绍了一个用于发送HTTP请求的工具类,包括POST、GET、DELETE、PATCH、PUT和OPTIONS等请求方式。该工具类使用Apache HttpClient进行网络通信,并利用JsonPoolUtils进行JSON解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.IOException;

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.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * HTTP请求辅助工具
 * 
 * @project iweixin-pay
 * @fileName WeixinUtil.java
 * @Description
 * @author light-zhang
 * @date 2018年5月29日下午3:29:42
 * @version 1.0.0
 */
public class HttpUtils {
    private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
    private static final HttpClient httpClient = HttpClientBuilder.create().build();

    /**
     * 发送POST请求
     * 
     * @param url
     * @param _class
     * @return
     */
    public static <T> T post(String url, Class<T> typeOfT) {
        try {
            HttpResponse response = httpClient.execute(new HttpPost(url));
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                logger.debug("post httpRequest url info:{},response info:{}", url, response);
                return JsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

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

    /**
     * 发送PATCH请求
     * 
     * @param url
     * @param typeOfT
     * @return
     */
    public static <T> T patch(String url, Class<T> typeOfT) {
        try {
            HttpResponse response = httpClient.execute(new HttpPatch(url));
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                logger.debug("patch httpRequest url info:{},response info:{}", url, response);
                return JsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发送GET请求
     * 
     * @param url
     * @param obj
     * @return
     */
    public static <T> T get(String url, Class<T> typeOfT) {
        try {
            HttpResponse response = httpClient.execute(new HttpGet(url));
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                logger.debug("get httpRequest url info:{},response info:{}", url, response);
                return JsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发送PUT请求
     * 
     * @param url
     * @param _class
     * @return
     */
    public static <T> T put(String url, Class<T> typeOfT) {
        try {
            HttpResponse response = httpClient.execute(new HttpPut(url));
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                logger.debug("put httpRequest url info:{},response info:{}", url, response);
                return JsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发送OPTIONS请求
     * 
     * @param url
     * @param typeOfT
     * @return
     */
    public static <T> T options(String url, Class<T> typeOfT) {
        try {
            HttpResponse response = httpClient.execute(new HttpOptions(url));
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                logger.debug("options httpRequest url info:{},response info:{}", url, response);
                return JsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

转载于:https://www.cnblogs.com/light-zhang/p/9869230.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值