分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

HttpUtils 发送HTTP/HTTPS/{get/post}请求

发布时间:2023-09-06 01:57责任编辑:熊小新关键词:暂无标签
import java.io.IOException;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.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.util.EntityUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.google.gson.Gson;/** * HTTP请求辅助工具 * ?* @project iweixin * @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); ???/** ????* 发送GET请求 ????* ?????* @param url ????* @param obj ????* @return ????*/ ???public static <T> T doGet(String url, Class<T> _class) { ???????HttpClient httpClient = HttpClientBuilder.create().build(); ???????HttpGet httpGet = new HttpGet(url); ???????try { ???????????HttpResponse response = httpClient.execute(httpGet); ???????????HttpEntity entity = response.getEntity(); ???????????if (entity != null) { ???????????????logger.debug("*********************doGet-URL****************" + url); ???????????????return new Gson().fromJson(EntityUtils.toString(entity, "utf-8"), _class); ???????????} ???????} catch (ClientProtocolException e) { ???????????e.printStackTrace(); ???????} catch (IOException e) { ???????????e.printStackTrace(); ???????} ???????return null; ???} ???/** ????* 发送POST请求 ????* ?????* @param url ????* @param _class ????* @return ????*/ ???public static <T> T doPost(String url, Class<T> _class) { ???????HttpClient httpClient = HttpClientBuilder.create().build(); ???????HttpPost httpPost = new HttpPost(url); ???????try { ???????????HttpResponse response = httpClient.execute(httpPost); ???????????HttpEntity entity = response.getEntity(); ???????????if (entity != null) { ???????????????logger.debug( ???????????????????????"*********************doPost-URL****************" + url); ???????????????return new Gson().fromJson(EntityUtils.toString(entity, "utf-8"), _class); ???????????} ???????} catch (ClientProtocolException e) { ???????????e.printStackTrace(); ???????} catch (IOException e) { ???????????e.printStackTrace(); ???????} ???????return null; ???}}

HttpUtils 发送HTTP/HTTPS/{get/post}请求

原文地址:https://www.cnblogs.com/light-zhang/p/9117769.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved