标签:HTTP 请求 url param 兼容 headers connection HTTPS return
依赖:
<dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.11.3</version> </dependency>
HttpUtils.java
package com.chao.http; import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; public class HttpUtils { /** * 请求超时时间 */ private static final int TIME_OUT = 120000; /** * Https请求 */ private static final String HTTPS = "https"; /** * Content-Type */ private static final String CONTENT_TYPE = "Content-Type"; /** * 表单提交方式Content-Type */ private static final String FORM_TYPE = "application/x-www-form-urlencoded;charset=UTF-8"; /** * JSON提交方式Content-Type */ private static final String JSON_TYPE = "application/json;charset=UTF-8"; /** * 发送get请求 (无参) * @param url * @return */ public static Connection.Response get(String url) throws IOException { return get(url,null); } /** * 发送get请求 * @param url * @param headers * @return */ public static Connection.Response get(String url,Map<String,String> headers) throws IOException { if (null == url || url.isEmpty()) { throw new RuntimeException("The request URL is blank."); } //如果是https请求 if(url.startsWith(HTTPS)){ getTrust(); } Connection connect = Jsoup.connect(url); connect.method(Connection.Method.GET); connect.timeout(TIME_OUT); connect.ignoreContentType(true); connect.ignoreContentType(true); connect.maxBodySize(0); if (headers != null) { connect.headers(headers); } Connection.Response response = connect.execute(); return response; } /** * 发送JSON格式参数POST请求 * @param url 请求路径 * @param params JSON格式请求参数 * @return HTTP响应对象 * @throws IOException 程序异常时抛出,由调用者处理 */ public static Connection.Response post(String url,String params) throws IOException { return doPostRequest(url,null,params); } /** * 发送JSON格式参数POST请求 * * @param url 请求路径 * @param headers 请求头中设置的参数 * @param params JSON格式请求参数 * @return HTTP响应对象 * @throws IOException 程序异常时抛出,由调用者处理 */ public static Connection.Response post(String url, Map<String, String> headers, String params) throws IOException { return doPostRequest(url, headers, params); } /** * 字符串参数post请求 * * @param url 请求URL地址 * @param paramMap 请求字符串参数集合 * @return HTTP响应对象 * @throws IOException 程序异常时抛出,由调用者处理 */ public static Connection.Response post(String url, Map<String, String> paramMap) throws IOException { return doPostRequest(url, null, paramMap, null); } /** * 带请求头的普通表单提交方式post请求 * * @param headers 请求头参数 * @param url 请求URL地址 * @param paramMap 请求字符串参数集合 * @return HTTP响应对象 * @throws IOException 程序异常时抛出,由调用者处理 */ public static Connection.Response post(Map<String, String> headers, String url, Map<String, String> paramMap) throws IOException { return doPostRequest(url, headers, paramMap, null); } /** * 执行post请求 * @param url 请求路径 * @param headers 请求头中设置的参数 * @param jsonParams JSON格式请求参数 * @return * @throws IOException */ private static Connection.Response doPostRequest(String url, Map<String, String> headers, String jsonParams) throws IOException { if (null == url || url.isEmpty()) { throw new RuntimeException("The request URL is blank."); } // 如果是Https请求 if (url.startsWith(HTTPS)) { getTrust(); } Connection connection = Jsoup.connect(url); connection.method(Connection.Method.POST); connection.timeout(TIME_OUT); connection.ignoreHttpErrors(true); connection.ignoreContentType(true); connection.maxBodySize(0); if (null != headers) { connection.headers(headers); } connection.header(CONTENT_TYPE,JSON_TYPE); connection.requestBody(jsonParams); Connection.Response response = connection.execute(); return response; } /** * 普通表单方式发送POST请求 * * @param url 请求URL地址 * @param headers 请求头 * @param paramMap 请求字符串参数集合 * @param fileMap 请求文件参数集合 * @return HTTP响应对象 * @throws IOException 程序异常时抛出,由调用者处理 */ private static Connection.Response doPostRequest(String url, Map<String, String> headers, Map<String, String> paramMap, Map<String, File> fileMap) throws IOException { if (null == url || url.isEmpty()) { throw new RuntimeException("The request URL is blank."); } // 如果是Https请求 if (url.startsWith(HTTPS)) { getTrust(); } Connection connection = Jsoup.connect(url); connection.method(Connection.Method.POST); connection.timeout(TIME_OUT); connection.ignoreHttpErrors(true); connection.ignoreContentType(true); connection.maxBodySize(0); if (null != headers) { connection.headers(headers); } // 收集上传文件输入流,最终全部关闭 List<InputStream> inputStreamList = null; try { // 添加文件参数 if (null != fileMap && !fileMap.isEmpty()) { inputStreamList = new ArrayList<InputStream>(); InputStream in = null; File file = null; for (Map.Entry<String, File> e : fileMap.entrySet()) { file = e.getValue(); in = new FileInputStream(file); inputStreamList.add(in); connection.data(e.getKey(), file.getName(), in); } } // 普通表单提交方式 else { connection.header(CONTENT_TYPE, FORM_TYPE); } // 添加字符串类参数 if (null != paramMap && !paramMap.isEmpty()) { connection.data(paramMap); } Connection.Response response = connection.execute(); return response; } // 关闭上传文件的输入流 finally { closeStream(inputStreamList); } } /** * 关流 * @param list */ private static void closeStream(List<InputStream> list) { if (Objects.isNull(list)){ list.stream().forEach(x -> { try { x.close(); } catch (IOException e) { e.printStackTrace(); } }); } } /** * 获取服务器信任 */ private static void getTrust(){ try { HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } }); SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null,new X509TrustManager[]{ new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }},new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); } catch (Exception e) { e.printStackTrace(); } } }
标签:HTTP,请求,url,param,兼容,headers,connection,HTTPS,return 来源: https://www.cnblogs.com/chao521/p/14314256.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。