直接粘贴复制,各种请求参数都支持,需要什么的请求方式就调用不同的方法,想你所想,写你所写

package net.bx.util;import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.lang3.StringUtils;import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;public class HTTPutil {private static final String DEFAULT_CHARSET = "UTF-8";/*** 发送Get请求* @param url* @return* @throws NoSuchProviderException * @throws NoSuchAlgorithmException * @throws IOException * @throws KeyManagementException */private static String getHeaderParam(String url, Map<String, String> headerParam) throws NoSuchAlgorithmException, NoSuchProviderException, IOException, KeyManagementException {StringBuffer bufferRes = null;TrustManager[] tm = { new MyX509TrustManager() };  SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");  sslContext.init(null, tm, new java.security.SecureRandom());  // 从上述SSLContext对象中得到SSLSocketFactory对象  SSLSocketFactory ssf = sslContext.getSocketFactory();URL urlGet = new URL(url);HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();if(headerParam!=null){for (String key : headerParam.keySet()) {http.addRequestProperty(key,headerParam.get(key));}}// 连接超时http.setConnectTimeout(25000);// 读取超时 --服务器响应比较慢,增大时间http.setReadTimeout(25000);http.setRequestMethod("GET");http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");http.setSSLSocketFactory(ssf);http.setDoOutput(true);http.setDoInput(true);http.connect();InputStream in = http.getInputStream();BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));String valueString = null;bufferRes = new StringBuffer();while ((valueString = read.readLine()) != null){bufferRes.append(valueString);}in.close();if (http != null) {// 关闭连接http.disconnect();}return bufferRes.toString();}/*** 发送Get请求* @param url* @return* @throws NoSuchProviderException * @throws NoSuchAlgorithmException * @throws IOException * @throws KeyManagementException */public static String get(String url,Boolean https,Map<String,String> headerParam) throws NoSuchAlgorithmException, NoSuchProviderException, IOException, KeyManagementException {if(https != null && https){return getHeaderParam(url, headerParam);}else{StringBuffer bufferRes = null;URL urlGet = new URL(url);HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();//请求头信息if(headerParam!=null){for (String key : headerParam.keySet()) {http.addRequestProperty(key,headerParam.get(key));}}// 连接超时http.setConnectTimeout(25000);// 读取超时 --服务器响应比较慢,增大时间http.setReadTimeout(25000);http.setRequestMethod("GET");http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");http.setDoOutput(true);http.setDoInput(true);http.connect();InputStream in = http.getInputStream();BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));String valueString = null;bufferRes = new StringBuffer();while ((valueString = read.readLine()) != null){bufferRes.append(valueString);}in.close();if (http != null) {// 关闭连接http.disconnect();}return bufferRes.toString();}}/***  发送Get请求* @param url* @param params* @return* @throws IOException * @throws NoSuchProviderException * @throws NoSuchAlgorithmException * @throws KeyManagementException */public static String get(String url, Map<String, String> params) throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {return getHeaderParam(initParams(url, params), null);}/***  发送Post请求* @param url* @param params* @return* @throws IOException * @throws NoSuchProviderException * @throws NoSuchAlgorithmException * @throws KeyManagementException */private static String post(String url, String params,Map<String,String> headerParam,String method) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {StringBuffer bufferRes = null;TrustManager[] tm = { new MyX509TrustManager() };SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");sslContext.init(null, tm, new java.security.SecureRandom());// 从上述SSLContext对象中得到SSLSocketFactory对象  SSLSocketFactory ssf = sslContext.getSocketFactory();URL urlGet = new URL(url);HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();if(headerParam!=null){for (String key : headerParam.keySet()) {http.addRequestProperty(key,headerParam.get(key));}}// 连接超时http.setConnectTimeout(25000);// 读取超时 --服务器响应比较慢,增大时间http.setReadTimeout(25000);http.setRequestMethod(method);http.setRequestProperty("Content-Type","application/json");http.setSSLSocketFactory(ssf);http.setDoOutput(true);http.setDoInput(true);http.connect();OutputStream out = http.getOutputStream();out.write(params.getBytes("UTF-8"));out.flush();out.close();InputStream in = http.getInputStream();BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));String valueString = null;bufferRes = new StringBuffer();while ((valueString = read.readLine()) != null){bufferRes.append(valueString);}in.close();if (http != null) {// 关闭连接http.disconnect();}return bufferRes.toString();}public static String post(String url, String params,Boolean https,Map<String,String> headerParam) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {return post(url,params,https,headerParam,"POST");}/***  发送Post请求* @param url* @param params* @return* @throws IOException * @throws NoSuchProviderException * @throws NoSuchAlgorithmException * @throws KeyManagementException */public static String post(String url, String params,Boolean https,Map<String,String> headerParam,String method) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {if(https != null && https){return post(url,params,headerParam,method);}else{StringBuffer bufferRes = null;URL urlPost = new URL(url);HttpURLConnection http = (HttpURLConnection) urlPost.openConnection();//请求头信息if(headerParam!=null){for (String key : headerParam.keySet()) {http.addRequestProperty(key,headerParam.get(key));}}// 连接超时http.setConnectTimeout(25000);// 读取超时 --服务器响应比较慢,增大时间http.setReadTimeout(25000);http.setRequestMethod(method);http.setRequestProperty("Content-Type","application/json");http.setDoOutput(true);http.setDoInput(true);http.connect();OutputStream out = http.getOutputStream();out.write(params.getBytes("UTF-8"));out.flush();out.close();InputStream in = http.getInputStream();BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));String valueString = null;bufferRes = new StringBuffer();while ((valueString = read.readLine()) != null){bufferRes.append(valueString);}in.close();if (http != null) {// 关闭连接http.disconnect();}return bufferRes.toString();}}/*** 构造url* @param url* @param params* @return*/private static String initParams(String url, Map<String, String> params){if (null == params || params.isEmpty()) {return url;}StringBuilder sb = new StringBuilder(url);if (url.indexOf("?") == -1) {sb.append("?");} else {sb.append("&");}boolean first = true;for (Entry<String, String> entry : params.entrySet()) {if (first) {first = false;} else {sb.append("&");}String key = entry.getKey();String value = entry.getValue();sb.append(key).append("=");if (StringUtils.isNotEmpty(value)) {try {sb.append(URLEncoder.encode(value, DEFAULT_CHARSET));} catch (UnsupportedEncodingException e) {e.printStackTrace();}}}return sb.toString();}/*** 发送Get请求* @param url* @return* @throws NoSuchProviderException * @throws NoSuchAlgorithmException * @throws IOException * @throws KeyManagementException */public static Map<String,String> getHeaders(String url) throws NoSuchAlgorithmException, NoSuchProviderException, IOException, KeyManagementException {Map<String,String> headers = new HashMap<String,String>();URL urlGet = new URL(url);HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();// 连接超时http.setConnectTimeout(25000);// 读取超时 --服务器响应比较慢,增大时间http.setReadTimeout(25000);http.setRequestMethod("GET");http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");http.setDoOutput(true);http.setDoInput(true);http.connect();headers.put("contentType",http.getContentType());if (http != null) {// 关闭连接http.disconnect();}return headers;}/***  发送Post请求* @param url* @param params* @return* @throws IOException* @throws NoSuchProviderException* @throws NoSuchAlgorithmException* @throws KeyManagementException*/public static String post(String url, String params,Boolean https) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {if(https != null && https){return post(url,params);}else{StringBuffer bufferRes = null;URL urlPost = new URL(url);HttpURLConnection http = (HttpURLConnection) urlPost.openConnection();// 连接超时http.setConnectTimeout(25000);// 读取超时 --服务器响应比较慢,增大时间http.setReadTimeout(25000);http.setRequestMethod("POST");http.setRequestProperty("Content-Type","application/json");http.setDoOutput(true);http.setDoInput(true);http.connect();OutputStream out = http.getOutputStream();out.write(params.getBytes("UTF-8"));out.flush();out.close();InputStream in = http.getInputStream();BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));String valueString = null;bufferRes = new StringBuffer();while ((valueString = read.readLine()) != null){bufferRes.append(valueString);}in.close();if (http != null) {// 关闭连接http.disconnect();}return bufferRes.toString();}}/***  发送Post请求* @param url* @param params* @return* @throws IOException* @throws NoSuchProviderException* @throws NoSuchAlgorithmException* @throws KeyManagementException*/public static String post(String url, String params) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {StringBuffer bufferRes = null;TrustManager[] tm = { new MyX509TrustManager() };SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");sslContext.init(null, tm, new java.security.SecureRandom());// 从上述SSLContext对象中得到SSLSocketFactory对象SSLSocketFactory ssf = sslContext.getSocketFactory();URL urlGet = new URL(url);HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();// 连接超时http.setConnectTimeout(25000);// 读取超时 --服务器响应比较慢,增大时间http.setReadTimeout(25000);http.setRequestMethod("POST");http.setRequestProperty("Content-Type","application/json");http.setRequestProperty("Accept","application/json");http.setRequestProperty("Authorization","WECHATPAY2-SHA256-RSA2048"+" "+params);http.setSSLSocketFactory(ssf);http.setDoOutput(true);http.setDoInput(true);http.connect();OutputStream out = http.getOutputStream();out.write(params.getBytes("UTF-8"));out.flush();out.close();InputStream in = http.getInputStream();BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));String valueString = null;bufferRes = new StringBuffer();while ((valueString = read.readLine()) != null){bufferRes.append(valueString);}in.close();if (http != null) {// 关闭连接http.disconnect();}return bufferRes.toString();}/*** 发送Get请求* @param url* @return* @throws NoSuchProviderException* @throws NoSuchAlgorithmException* @throws IOException* @throws KeyManagementException*/private static String get(String url) throws NoSuchAlgorithmException, NoSuchProviderException, IOException, KeyManagementException {StringBuffer bufferRes = null;TrustManager[] tm = { new MyX509TrustManager() };SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");sslContext.init(null, tm, new java.security.SecureRandom());// 从上述SSLContext对象中得到SSLSocketFactory对象SSLSocketFactory ssf = sslContext.getSocketFactory();URL urlGet = new URL(url);HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();// 连接超时http.setConnectTimeout(25000);// 读取超时 --服务器响应比较慢,增大时间http.setReadTimeout(25000);http.setRequestMethod("GET");http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");http.setSSLSocketFactory(ssf);http.setDoOutput(true);http.setDoInput(true);http.connect();InputStream in = http.getInputStream();BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));String valueString = null;bufferRes = new StringBuffer();while ((valueString = read.readLine()) != null){bufferRes.append(valueString);}in.close();if (http != null) {// 关闭连接http.disconnect();}return bufferRes.toString();}/*** 发送Get请求* @param url* @return* @throws NoSuchProviderException* @throws NoSuchAlgorithmException* @throws IOException* @throws KeyManagementException*/public static String get(String url,Boolean https) throws NoSuchAlgorithmException, NoSuchProviderException, IOException, KeyManagementException {if(https != null && https){return get(url);}else{StringBuffer bufferRes = null;URL urlGet = new URL(url);HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();// 连接超时http.setConnectTimeout(25000);// 读取超时 --服务器响应比较慢,增大时间http.setReadTimeout(25000);http.setRequestMethod("GET");http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");http.setDoOutput(true);http.setDoInput(true);http.connect();InputStream in = http.getInputStream();BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));String valueString = null;bufferRes = new StringBuffer();while ((valueString = read.readLine()) != null){bufferRes.append(valueString);}in.close();if (http != null) {// 关闭连接http.disconnect();}return bufferRes.toString();}}public static String postUrlJson(String url, String params) throws Exception {Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443);Protocol.registerProtocol("https", myhttps);HttpClient client = new HttpClient();HttpMethod post = new PostMethod(url + "?" + params);try {client.executeMethod(post);byte[] responseBody = post.getResponseBody();String result = new String(responseBody,"UTF-8");return result;} finally {post.releaseConnection();}}
}

关于java的http请求的工具类相关推荐

  1. 前端请求接口post_程序员:HttpClient进行post请求的工具类,访问第三方接口HTTPS...

    HTTPS (英语:Hypertext Transfer Protocol Secure,缩写:HTTPS,常称为HTTP over TLS,HTTP over SSL或HTTP Secure) 是一 ...

  2. JAVA 文件上传下载工具类

    JAVA 文件上传下载工具类 import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org. ...

  3. 超好用的后端发送http请求HttpUtils工具类(基于原生http连接,不需要另外导包)

    在项目中,为了实现一些特定的功能,我们常常需要发送http异步请求 ,为此需要特意封装一个实用的HttpUtils工具类 HttpUtils工具类内容如下: package com.zyw.secki ...

  4. java 项目中常用的工具类总结

    1.文件 1.根据图片的链接,下载图片 package com.lingxu.module.BigDataJoinMessage.util;import java.io.FileOutputStrea ...

  5. java企业微信消息发送工具类

    java企业微信消息发送工具类 用途 基于java实现调用企业微信发送消息,文件,图片. 实现效果 文件列表 文件名 用途 WechatUtil.java 企业微信工具类 WechatTest.jav ...

  6. 分享一个Java生成二维码工具类

    分享一个Java生成二维码工具类 直接上代码: 1.CodeUtil.class package top.lrshuai.blog.util;import java.awt.BasicStroke; ...

  7. JavaSocket编写发送TCP请求的工具类

    转自:https://blog.csdn.net/jadyer/article/details/8788303 package com.jadyer.util;import java.io.ByteA ...

  8. Http请求-hutool工具类的使用

    Http请求-hutool工具类的使用 前言 在日常java后端开发的工作中经常会遇到后端发起HTTP请求的情况,这里可以使用一个简单的工具类. 官方网址:Http请求-HttpRequest (hu ...

  9. 分享一个发送http请求的工具类

    分享一个发送http请求的工具类 maven依赖只需要导入一个 <dependencies><dependency><groupId>commons-httpcli ...

  10. java inputtools_Java后台开发常用工具类

    本文涉及的工具类部分是自己编写,另一部分是在项目里收集的.工具类涉及数据库连接.格式转换.文件操作.发送邮件等等.提高开发效率,欢迎收藏与转载. 数据库连接工具类 数据库连接工具类--仅仅获得连接对象 ...

最新文章

  1. 【leetcode记录01】 数组
  2. Hadoop Ecosystem解决方案---数据仓库
  3. POJ 1321 棋盘问题(回溯)
  4. python单词去重及排序_Python实现对文件进行单词划分并去重排序操作示例
  5. 七、制作主题(二) Anatomy of a theme
  6. 使用 ASM 实现 Java 语言的“多重继承”
  7. 内容编辑器在MOSS中的应用
  8. 计算机制作ppt教程,电脑制作ppt详细步骤_新手制作ppt详细步骤图文教程
  9. jersey2 java_无废话Jersey构建RESTful服务之WebService系统教程 --2 [JAVA对象转换成XML输出]...
  10. Node2vec原理剖析,代码实现
  11. python好玩的代码-python有趣的一行代码
  12. 信号处理的算法matlab,基于MATLAB的信号处理常用算法的GUI界面设计(程序)
  13. 利用1stOpt进行方程拟合与参数优化
  14. 第九话 树结构实际应用
  15. 股权转让是什么?如何股权转让?
  16. 拼多多新年大促活动规则介绍,拓商科技助力店铺突围
  17. 个人微信小程序云开发总结心得
  18. Git 经验总结及 Git GitHub 学习指南
  19. 决策树与剪枝、bagging与随机森林、极端随机树、Adaboost、GBDT算法原理详解
  20. Java秒杀系统方案优化【第三方登录】

热门文章

  1. 快速集成 iMessage(下)
  2. 1.按钮点击的四种方式
  3. 一种去除U盘写保护的可行方法(dd 命令解决)
  4. VTK学习笔记(二十三)vtk空间几何变换
  5. 全年营业额怎么计算_年度利润总额怎么算?
  6. log4j2关闭debug日志
  7. matlab最后ceq,求解 minimax 约束问题
  8. 无法修改IE浏览器主页解决方案(主页绑架)
  9. VMware5.5-VMware补丁程序VUM
  10. PS大神最全脑洞合集