java 发送POST,DELETE,PATCH,GET请求
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java 发送POST,DELETE,PATCH,GET请求,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4427字,纯文字阅读大概需要7分钟。
内容图文
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); privatestaticfinal HttpClient httpClient = HttpClientBuilder.create().build(); /** * 发送POST请求 * * @param url * @param _class * @return*/publicstatic <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(); } returnnull; } /** * 发送DELETE请求 * * @param url * @param typeOfT * @return*/publicstatic <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(); } returnnull; } /** * 发送PATCH请求 * * @param url * @param typeOfT * @return*/publicstatic <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(); } returnnull; } /** * 发送GET请求 * * @param url * @param obj * @return*/publicstatic <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(); } returnnull; } /** * 发送PUT请求 * * @param url * @param _class * @return*/publicstatic <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(); } returnnull; } /** * 发送OPTIONS请求 * * @param url * @param typeOfT * @return*/publicstatic <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(); } returnnull; } }
原文:https://www.cnblogs.com/light-zhang/p/9869230.html
内容总结
以上是互联网集市为您收集整理的java 发送POST,DELETE,PATCH,GET请求全部内容,希望文章能够帮你解决java 发送POST,DELETE,PATCH,GET请求所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。