package com.example.demo.controller;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.FormBodyPart;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.impl.cookie.BestMatchSpec;
import org.apache.http.message.BasicNameValuePair;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;
/**
* Http帮助类,用于发送http请求
*/
public class HttpHelper {
private static final Logger log = LoggerFactory.getLogger(HttpHelper.class);
private static final int bufferSize = 1024;
private static final String defaultEncoding = "UTF-8";
private static volatile HttpHelper instance;
/**
* 单例方法
*
* @return HttpHelper
*/
public static HttpHelper getInstance() {
synchronized (HttpHelper.class) {
if (HttpHelper.instance == null) {
instance = new HttpHelper();
}
return instance;
}
}
/**
* 重新创建新的单例方法
*
* @return HttpHelper
*/
public static HttpHelper newInstance() {
synchronized (HttpHelper.class) {
HttpHelper.instance = new HttpHelper();
}
return instance;
}
private volatile HttpClient client;
private volatile BasicCookieStore cookieStore;
private Map<String, String> headerParams = new HashMap<>();
public HttpHelper() {
// 设置连接参数
ConnectionConfig connConfig = ConnectionConfig.custom().setCharset(Charset.forName(defaultEncoding)).build();
SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(100000).build();
RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
ConnectionSocketFactory plainSF = new PlainConnectionSocketFactory();
registryBuilder.register("http", plainSF);
// 指定信任密钥存储对象和连接套接字工厂
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
SSLContext sslContext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, new AnyTrustStrategy()).build();
LayeredConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
registryBuilder.register("https", sslSF);
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
throw new RuntimeException(e);
}
Registry<ConnectionSocketFactory> registry = registryBuilder.build();
// 设置连接管理器
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
connManager.setDefaultConnectionConfig(connConfig);
connManager.setDefaultSocketConfig(socketConfig);
// 指定cookie存储对象
cookieStore = new BasicCookieStore();
// 构建客户端
client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).setConnectionManager(connManager).build();
}
public void setHeaderParam(String key, String value) {
if (key != null && value != null) {
headerParams.put(key, value);
}
}
public void clearHeaderParams() {
this.headerParams.clear();
}
private static List<NameValuePair> paramsConverter(Map<String, String> params) {
List<NameValuePair> nvps = new LinkedList<>();
Set<Map.Entry<String, String>> paramsSet = params.entrySet();
for (Map.Entry<String, String> paramEntry : paramsSet) {
nvps.add(new BasicNameValuePair(paramEntry.getKey(), paramEntry.getValue()));
}
return nvps;
}
public static String readStream(InputStream in, String encoding) {
if (in == null) {
return null;
}
InputStreamReader inReader = null;
try {
if (encoding == null) {
inReader = new InputStreamReader(in, defaultEncoding);
} else {
inReader = new InputStreamReader(in, encoding);
}
char[] buffer = new char[bufferSize];
int readLen;
StringBuilder sb = new StringBuilder();
while ((readLen = inReader.read(buffer)) != -1) {
sb.append(buffer, 0, readLen);
}
return sb.toString();
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(inReader);
}
return null;
}
// ==============================================================================================================
// ============================================ GET =============================================================
// ==============================================================================================================
public InputStream doGet(String url) throws URISyntaxException, ClientProtocolException, IOException {
HttpResponse response = this.doGet(url, null);
return response != null ? response.getEntity().getContent() : null;
}
public HttpResponse doGet(String url, Map<String, String> queryParams) throws URISyntaxException, ClientProtocolException, IOException {
HttpGet gm = new HttpGet();
URIBuilder builder = new URIBuilder(url);
//填入查询参数
if (queryParams != null && !queryParams.isEmpty()) {
builder.setParameters(HttpHelper.paramsConverter(queryParams));
}
// header
if (this.headerParams != null && this.headerParams.size() > 0) {
for (String key : this.headerParams.keySet()) {
gm.setHeader(key, this.headerParams.get(key));
}
}
gm.setURI(builder.build());
return client.execute(gm);
}
public String doGetForString(String url) throws URISyntaxException, ClientProtocolException, IOException {
return HttpHelper.readStream(this.doGet(url), null);
}
public InputStream doGetForStream(String url, Map<String, String
没有合适的资源?快使用搜索试试~ 我知道了~
restful zip请求直接解析和下载
共87个文件
xml:71个
class:5个
java:3个
需积分: 9 11 下载量 200 浏览量
2018-08-11
14:08:47
上传
评论
收藏 93KB RAR 举报
温馨提示
restful zip请求直接解析和下载restful zip请求直接解析和下载restful zip请求直接解析和下载restful zip请求直接解析和下载restful zip请求直接解析和下载restful zip请求直接解析和下载restful zip请求直接解析和下载restful zip请求直接解析和下载
资源推荐
资源详情
资源评论
收起资源包目录
demo.rar (87个子文件)
demo
.gitignore 268B
.mvn
wrapper
maven-wrapper.jar 46KB
maven-wrapper.properties 110B
pom.xml 2KB
mvnw 6KB
target
generated-test-sources
test-annotations
test-classes
generated-sources
annotations
classes
com
example
demo
controller
HttpHelper.class 19KB
HttpHelper$1.class 234B
ZipTestController.class 5KB
HttpHelper$AnyTrustStrategy.class 1012B
DemoApplication.class 703B
application.properties 0B
demo.iml 8KB
src
test
java
com
example
demo
main
resources
static
templates
application.properties 0B
java
com
example
demo
controller
ZipTestController.java 5KB
HttpHelper.java 18KB
DemoApplication.java 316B
mvnw.cmd 5KB
.idea
libraries
Maven__org_springframework_spring_context_5_0_8_RELEASE.xml 637B
Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml 700B
Maven__org_springframework_boot_spring_boot_test_2_0_4_RELEASE.xml 671B
Maven__org_hamcrest_hamcrest_core_1_3.xml 532B
Maven__net_minidev_accessors_smart_1_2.xml 542B
Maven__javax_validation_validation_api_2_0_1_Final.xml 611B
Maven__org_springframework_boot_spring_boot_starter_tomcat_2_0_4_RELEASE.xml 741B
Maven__org_springframework_spring_core_5_0_8_RELEASE.xml 616B
Maven__net_minidev_json_smart_2_3.xml 507B
Maven__org_springframework_boot_spring_boot_starter_json_2_0_4_RELEASE.xml 727B
Maven__com_fasterxml_jackson_core_jackson_annotations_2_9_0.xml 644B
Maven__org_apache_tomcat_embed_tomcat_embed_el_8_5_32.xml 611B
Maven__org_apache_httpcomponents_httpmime_4_3_5.xml 563B
Maven__org_apache_tomcat_embed_tomcat_embed_core_8_5_32.xml 625B
Maven__org_jboss_logging_jboss_logging_3_3_2_Final.xml 608B
Maven__org_springframework_spring_jcl_5_0_8_RELEASE.xml 609B
Maven__org_springframework_boot_spring_boot_starter_logging_2_0_4_RELEASE.xml 748B
Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_0_4_RELEASE.xml 769B
Maven__org_springframework_boot_spring_boot_autoconfigure_2_0_4_RELEASE.xml 734B
Maven__com_fasterxml_classmate_1_3_4.xml 522B
Maven__org_hibernate_validator_hibernate_validator_6_0_11_Final.xml 681B
Maven__org_springframework_spring_test_5_0_8_RELEASE.xml 616B
Maven__org_springframework_spring_aop_5_0_8_RELEASE.xml 609B
Maven__org_ow2_asm_asm_5_0_4.xml 472B
Maven__org_hamcrest_hamcrest_library_1_3.xml 553B
Maven__org_springframework_restdocs_spring_restdocs_mockmvc_2_0_2_RELEASE.xml 736B
Maven__org_apache_tomcat_embed_tomcat_embed_websocket_8_5_32.xml 660B
Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_9_6.xml 729B
Maven__ch_qos_logback_logback_core_1_2_3.xml 547B
Maven__com_fasterxml_jackson_core_jackson_core_2_9_6.xml 595B
Maven__org_objenesis_objenesis_2_6.xml 508B
Maven__ch_qos_logback_logback_classic_1_2_3.xml 568B
Maven__org_springframework_spring_web_5_0_8_RELEASE.xml 609B
Maven__com_jayway_jsonpath_json_path_2_4_0.xml 546B
Maven__org_springframework_boot_spring_boot_starter_web_2_0_4_RELEASE.xml 720B
Maven__commons_codec_commons_codec_1_11.xml 543B
Maven__javax_annotation_javax_annotation_api_1_3_2.xml 611B
Maven__org_springframework_restdocs_spring_restdocs_core_2_0_2_RELEASE.xml 715B
Maven__junit_junit_4_12.xml 455B
Maven__org_slf4j_jul_to_slf4j_1_7_25.xml 534B
Maven__com_fasterxml_jackson_core_jackson_databind_2_9_6.xml 623B
Maven__org_slf4j_slf4j_api_1_7_25.xml 513B
Maven__org_apache_logging_log4j_log4j_to_slf4j_2_10_0.xml 608B
Maven__org_assertj_assertj_core_3_9_1.xml 535B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jdk8_2_9_6.xml 674B
Maven__org_skyscreamer_jsonassert_1_5_0.xml 537B
Maven__org_springframework_spring_expression_5_0_8_RELEASE.xml 658B
Maven__net_bytebuddy_byte_buddy_agent_1_7_11.xml 578B
Maven__org_apache_httpcomponents_httpcore_4_4_10.xml 570B
Maven__org_xmlunit_xmlunit_core_2_5_1.xml 535B
Maven__org_mockito_mockito_core_2_15_0.xml 542B
Maven__org_springframework_boot_spring_boot_starter_2_0_4_RELEASE.xml 692B
Maven__org_springframework_spring_beans_5_0_8_RELEASE.xml 623B
Maven__javax_servlet_javax_servlet_api_3_1_0.xml 578B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jsr310_2_9_6.xml 688B
Maven__org_apache_httpcomponents_httpclient_4_3_5.xml 577B
Maven__org_springframework_spring_webmvc_5_0_8_RELEASE.xml 630B
Maven__org_springframework_boot_spring_boot_starter_test_2_0_4_RELEASE.xml 727B
Maven__net_bytebuddy_byte_buddy_1_7_11.xml 536B
Maven__org_yaml_snakeyaml_1_19.xml 495B
Maven__org_springframework_boot_spring_boot_2_0_4_RELEASE.xml 636B
Maven__org_apache_logging_log4j_log4j_api_2_10_0.xml 573B
workspace.xml 29KB
encodings.xml 215B
dictionaries
admin.xml 86B
misc.xml 728B
modules.xml 248B
compiler.xml 620B
codeStyles
Project.xml 268B
codeStyleConfig.xml 158B
共 87 条
- 1
资源评论
xlj3
- 粉丝: 188
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- (源码)基于Spring Boot框架的博客系统.zip
- (源码)基于Spring Boot框架的博客管理系统.zip
- (源码)基于ESP8266和Blynk的IR设备控制系统.zip
- (源码)基于Java和JSP的校园论坛系统.zip
- (源码)基于ROS Kinetic框架的AGV激光雷达导航与SLAM系统.zip
- (源码)基于PythonDjango框架的资产管理系统.zip
- (源码)基于计算机系统原理与Arduino技术的学习平台.zip
- (源码)基于SSM框架的大学消息通知系统服务端.zip
- (源码)基于Java Servlet的学生信息管理系统.zip
- (源码)基于Qt和AVR的FestosMechatronics系统终端.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功