当前位置: 首页 > news >正文

建站免费加盟色盲测试图数字

建站免费加盟,色盲测试图数字,wordpress突然变慢,百度网站制作公司首先明确两个事情:请求对象,连接对象 我们知道你要是想发起一个请求,需要指定两个环节内容,一个是请求内容对象(request),一个是连接内容对象(httpClient) 它们两个的作用我们在下面会看到 简要分析源码 1.先说一下…

首先明确两个事情:请求对象,连接对象

我们知道你要是想发起一个请求,需要指定两个环节内容,一个是请求内容对象(request),一个是连接内容对象(httpClient)
它们两个的作用我们在下面会看到

简要分析源码

1.先说一下结论,spring所有的核心代码都在doxxx()方法里面,而http请求的核心代码在doExecute()中。

# 我们平时会写这个一个方法去开启http请求调用
restTemplate.postForObject(url,httpEntity, xxx.class);# 往里钻
@Nullablepublic <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables) throws RestClientException {RequestCallback requestCallback = this.httpEntityCallback(request, responseType);HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor(responseType, this.getMessageConverters(), this.logger);return this.execute(url, HttpMethod.POST, requestCallback, responseExtractor, (Object[])uriVariables);}#继续钻,发现了doxxx()方法
@Nullablepublic <T> T execute(String url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, Object... uriVariables) throws RestClientException {URI expanded = this.getUriTemplateHandler().expand(url, uriVariables);return this.doExecute(expanded, method, requestCallback, responseExtractor);}#看看实现,我们会发现有一个创建request的操作
@Nullableprotected <T> T doExecute(URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {Assert.notNull(url, "URI is required");Assert.notNull(method, "HttpMethod is required");ClientHttpResponse response = null;Object var14;try {# 核心!!!ClientHttpRequest request = this.createRequest(url, method);if (requestCallback != null) {requestCallback.doWithRequest(request);}response = request.execute();this.handleResponse(url, method, response);var14 = responseExtractor != null ? responseExtractor.extractData(response) : null;} catch (IOException var12) {String resource = url.toString();String query = url.getRawQuery();resource = query != null ? resource.substring(0, resource.indexOf(63)) : resource;throw new ResourceAccessException("I/O error on " + method.name() + " request for \"" + resource + "\": " + var12.getMessage(), var12);} finally {if (response != null) {response.close();}}return var14;}# 我们发现所有的request对象都是通过factory创建的,不同的factory会创建不同的request对象
# 因为目前我们位于抽象类HttpAccessor中,所以我们要继续往实现类追踪getRequestFactory()方法
protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException {ClientHttpRequest request = this.getRequestFactory().createRequest(url, method);this.initialize(request);if (this.logger.isDebugEnabled()) {this.logger.debug("HTTP " + method.name() + " " + url);}return request;}# 此时我们位于InterceptingHttpAccessor抽象类中,继续往下追踪就是RestInterceptors类了,没有重写getRequestFactory()方法,所以我们要重点关注InterceptingHttpAccessor抽象类中的重写逻辑:
public ClientHttpRequestFactory getRequestFactory() {List<ClientHttpRequestInterceptor> interceptors = this.getInterceptors();if (!CollectionUtils.isEmpty(interceptors)) {ClientHttpRequestFactory factory = this.interceptingRequestFactory;if (factory == null) {// 如果有interceptors,则融合父类的factory和interceptors,返回一个新的factory来覆盖原有父类factoryfactory = new InterceptingClientHttpRequestFactory(super.getRequestFactory(), interceptors);this.interceptingRequestFactory = (ClientHttpRequestFactory)factory;}return (ClientHttpRequestFactory)factory;} else {// 如果没有则interceptors,则直接用父类中的factory,return super.getRequestFactory();}}

至此,我们可以得到以下结论,如果想对原有请求进行扩展,我们需要从两个对象进行下手:factory,interceptor。

接下来我们看一下factory和interceptor两个类中都有什么内容:

#factory
public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequestFactory, DisposableBean {// 重点关注!!!连接对象private HttpClient httpClient;@Nullableprivate RequestConfig requestConfig;private boolean bufferRequestBody = true;@Nullableprivate BiFunction<HttpMethod, URI, HttpContext> httpContextFactory;
}#interceptor
public interface ClientHttpRequestInterceptor {// 重点关注!!!request对象ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException;
}

我们可以发现在factory中我们可以对连接对象进行修改,在interceptor中可以对请求对象进行修改;我们回归下文章开头,一个请求的两个组成部分我们已经发现了。接下来就演示下不同场景应该怎么使用这两种对象。

场景1:添加固定请求头

分析:请求头内容属于请求对象,所以我们通过interceptor来实现

@Configuration
public class RestTemplateConfig {/*** restTemplate*/@ConditionalOnMissingBean@Beanpublic RestTemplate restTemplate(ClientHttpRequestFactory factory) {RestTemplate restTemplate = new RestTemplate();ClientHttpRequestInterceptor clientHttpRequestInterceptor = new ClientHttpRequestInterceptor() {@Overridepublic ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)throws IOException {request.getHeaders().set("X-ID", "");request.getHeaders().set("X-APPKEY", "");return execution.execute(request, body);}};restTemplate.setInterceptors(Collections.singletonList(clientHttpRequestInterceptor));return restTemplate;}
}

场景二:添加请求证书

分析:请求头内容属于连接对象,所以我们通过factory来实现

public static HttpComponentsClientHttpRequestFactory generateHttpRequestFactory() {TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true;SSLContext sslContext = null;try {sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy)// 增加请求证书.loadKeyMaterial(ks, keyStorePassword.toCharArray()).setProtocol("TLSv1.2").build();} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {log.error("generateHttpRequestFactory failed:", e);}SSLConnectionSocketFactory connectionSocketFactory =new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());HttpClientBuilder httpClientBuilder = HttpClients.custom();httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);CloseableHttpClient httpClient = httpClientBuilder.build();HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();factory.setHttpClient(httpClient);factory.setConnectTimeout(15000);factory.setReadTimeout(5000);return factory;
}

当然两者可以同时存在:

public RestTemplate restTemplate(ClientHttpRequestFactory factory) {// 修改factoryRestTemplate restTemplate = new RestTemplate(generateHttpRequestFactory());ClientHttpRequestInterceptor clientHttpRequestInterceptor = new ClientHttpRequestInterceptor() {@Overridepublic ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)throws IOException {request.getHeaders().set("X-HW-ID", "com.huawei.osec");request.getHeaders().set("X-HW-APPKEY", "/D2QodV7Lu2EUk4D9HEUsQ==");return execution.execute(request, body);}};// 修改interceptorrestTemplate.setInterceptors(Collections.singletonList(clientHttpRequestInterceptor));return restTemplate;}
http://www.mmbaike.com/news/66379.html

相关文章:

  • 住房和城市建设部网站网络推广运营主要做什么
  • 建设公司网站方案bt磁力搜索引擎在线
  • 宁波高端网站建设推广快速申请免费个人网站
  • 南京哪家做电商网站经典广告
  • 我想做一个网站怎么做的网络营销推广方案3篇
  • 建设一个网站需要多久多少钱惠州seo优化
  • 做网站需要哪方面的编程淘宝怎么提高关键词搜索排名
  • 公司做网站的流程网站建设流程是什么
  • 怎么做视频聊天网站杭州网站推广优化
  • 自学网官方网站入口做一个公司网站大概要多少钱
  • 做服装设计看哪些网站企业宣传视频
  • 网站技术报务费如何做会计分录百度搜索竞价推广
  • 如何做网站的搜索优化指的是什么
  • 阿里云服务器 个人网站百度热搜关键词排名
  • 网站如何提交给百度网站宣传和推广的方法有哪些
  • 区县12380网站建设情况杭州百度首页排名
  • 免费php网站网站整合营销推广
  • 凡科网可以自己做网站吗百度宁波运营中心
  • 做网站能用自己电脑吗百度指数app
  • 联通 网站备案seo关键词排名优化案例
  • 在征婚网站上认识做期货济南做网站比较好的公司
  • 网站制作视频教程下载百度云软文模板app
  • 常州网站建设企业网站电脑优化大师下载安装
  • 顺德公司做网站seo网站优化培训公司
  • wordpress调用评论西安seo建站
  • c 语言做网站优化大师是干什么的
  • 长春做网站价格办公软件速成培训班
  • tp5网站开发逻辑架构软文宣传
  • 个人单页网站建设品牌整合营销案例
  • 图片展示型网站手机打开国外网站app