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

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

时间: 2024-08-08 20:36:26

java 发送POST,DELETE,PATCH,GET请求的相关文章

java 发送http 的get|post 请求

<div> package wzh.Http; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; public

java 发送带cookie的http请求

try{     String path = "https://www.AA.com/AA";         URL url = new URL(path);         HttpURLConnection con = (HttpURLConnection) url.openConnection();         con.setRequestMethod("GET");         /*con.setRequestProperty("Cont

关于JAVA发送Https请求(HttpsURLConnection和HttpURLConnection)

关于JAVA发送Https请求(HttpsURLConnection和HttpURLConnection) [转] https协议对于开发者而言其实只是多了一步证书验证的过程.这个证书正常情况下被jdk/jre/security/cacerts所管理.里面证书包含两种情况: 1.机构所颁发的被认证的证书,这种证书的网站在浏览器访问时https头显示为绿色如百度 2.个人所设定的证书,这种证书的网站在浏览器里https头显示为红色×,且需要点击信任该网站才能继续访问.而点击信任这一步的操作就是我们

Java发送HTTPS请求

前言 上篇文章介绍了 java 发送 http 请求,大家都知道发送http是不安全的 .我也是由于对接了其他企业后总结了一套发送 https的工具.大家网上找方法很多的,但是可不是你粘过来就能用啊,我也是踩过坑的,所以我这个工具,只要粘贴到你们自己项目里就可以用.我的工具跟网上没什么区别,唯一的区别是我亲身实战过,把需要注意的细节列出来,不让大家浪费时间.   正文 本文只介绍 发送 post 请求,既然选择了 https 就不会用get,因为get也是不安全的.   读前须知 我会把需要依赖

springmvc的POST 请求转为 DELETE 或 put 请求配置HiddenHttpMethodFilter

1.web.xml里配置 <!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELETE 或 put 请求 --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpM

Spring MVC添加支持Http的delete、put请求!(HiddenHttpMethodFilter)

浏览器form表单只支持GET与POST请求,而DELETE.PUT等method并不支持,spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET.POST.PUT与DELETE请求. 1.配置springmvc配置文件springmvc-servlet.xml<!-- 浏览器不支持put,delete等method,由该filter将/xxx?_method=delete转换为标准的http delete方法 -->  <filter>    

java发送exchange邮件问题

最近工作中遇到一个问题,本来很简单的一个问题,困扰了我2周 具体:java发送会议邮件到exchange服务器 奇怪:系统有80多家客户,基本大半都有会议邮件的发送,不管是outlook,网易闪电邮,Foxmail另外wps邮箱都可以接收到会以邮件 只有exchange不能接收到会议邮件,收到的只是 BEGIN:VCALENDAR PRODID:-//Events Calendar//iCal4j 1.0//EN CALSCALE:GREGORIAN VERSION:2.0 METHOD:REQ

Java学习路线分享SpringMVC之请求和响应

Java学习路线分享SpringMVC之请求和响应,前面我们学习了SpringMVC的基本配置,接下来一个非常重要的知识点是如何接受用户的请求以及如何将数据发送给用户. 获得请求参数 获得页面参数的几种方式 1)通过参数名获得 给控制器的方法设置参数名和表单name相同 2)通过@RequestParam("参数名")注解设置参数 @RequestParam("表单元素的name") 参数类型 参数名 3)自动装箱,创建属性名和表单名称一样的类 把类作为方法的参数

php如何发起POST DELETE GET POST 请求

get:是用来取得数据.其要传递过的信息是拼在url后面,因为其功能使然,有长度的限制 post:是用来上传数据.要上传的数据放在request的head里.没有长度限制.主要是用于增加操作 put:也是用来上传数据.但是一般是用在具体的资源上.主要用于修改操作 delete:用来删除某一具体的资源上 发起POST DELETE GET POST 请求通用类 <?php class commonFunction{ function callInterfaceCommon($URL,$type,$