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

做一个像美团的网站需要多少钱海报在线设计网站

做一个像美团的网站需要多少钱,海报在线设计网站,帝国网站后台编辑器没有了,wordpress怎样进入后台首先明确两个事情#xff1a;请求对象#xff0c;连接对象 我们知道你要是想发起一个请求#xff0c;需要指定两个环节内容#xff0c;一个是请求内容对象(request)#xff0c;一个是连接内容对象(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, ClassT responseType, Object... uriVariables) throws RestClientException {RequestCallback requestCallback this.httpEntityCallback(request, responseType);HttpMessageConverterExtractorT 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 ResponseExtractorT 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 ResponseExtractorT 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() {ListClientHttpRequestInterceptor 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则直接用父类中的factoryreturn super.getRequestFactory();}}至此我们可以得到以下结论如果想对原有请求进行扩展我们需要从两个对象进行下手factoryinterceptor。 接下来我们看一下factory和interceptor两个类中都有什么内容 #factory public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequestFactory, DisposableBean {// 重点关注连接对象private HttpClient httpClient;Nullableprivate RequestConfig requestConfig;private boolean bufferRequestBody true;Nullableprivate BiFunctionHttpMethod, 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*/ConditionalOnMissingBeanBeanpublic 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.yutouwan.com/news/25720/

相关文章:

  • 公司内部网站怎么建设南京seo关键词排名
  • 如何开发wordpress潍坊网站优化
  • 互联网行业网站设计周口集团网站建设
  • 域名备案好了怎么建设网站长沙网站建设服务公司
  • 相亲网站做期货现货贵金属的人博客做资讯类网站
  • 怎么弄自己的网站卖东西信誉好的丹阳网站建设
  • 网页设计需求分析搜狗seo快速排名公司
  • 教育类网站模板最新软件开发国家标准
  • 武昌做网站多少钱asp网站后台制作
  • 用户上传网站用什么做做企业网站收费价格
  • 广州高端品牌网站建设后台管理便捷石家庄简单的网页制作
  • 家具展示网站源码附近有木有做网站
  • 网站如何做付费wordpress download monitor
  • 张家界网站建设要求网站维护是怎么回事
  • 做软件下载网站有哪些网站建设 岗位
  • 地方网站做哪些内容投诉网站建设
  • 做印刷品的素材网站安阳如何优化网站
  • 安徽整站优化装修公司报价
  • 平顶山河南网站建设wordpress 网校插件
  • 张家界城乡建设网站广州市黄埔区建设局网站
  • 衡阳市建设局网站淄博网络宣传
  • 上海网站建设 普送vi设计公司排行榜
  • 阳逻开发区网站建设中企动力可以在线做c语言的网站
  • 什么是理财北京网站建设公司邯郸市人社局
  • 外贸seo网站建设共享办公都有哪些公司
  • 长春专业企业网站建设工作室网站代运营公司有哪些
  • 2015百度竞价单页面网站模板源码设计室内设计联盟网页版
  • 免费ps模板下载网站建立网站需要注意事项
  • 深圳网站页面设计公司网络服务遇到问题请检查网络状况或稍后再试吧
  • 一_建设网站前的市场分析网页制作考试题及答案