如何使用新的Apache Http Client发出HEAD请求
如果您已更新Apache HTTP Client代碼以使用最新的庫(在撰寫本文時,它是4.2.x版本的httpclient 4.3.5版本和httpcore 4.3.2版本),您會注意到某些類(例如org.apache.http.impl.client.DefaultHttpClient或org.apache.http.params.HttpParams已被棄用。 好吧,我去過那里,所以在這篇文章中,我將介紹如何通過使用新類擺脫警告。
1.
我將用于演示的用例很簡單:我有一個批處理作業(yè),以檢查是否有新的情節(jié)可用于播客。 為了避免在沒有新情節(jié)的情況下必須獲取和解析提要,我先驗證自上次調(diào)用以來eTag或提要資源的last-modified標頭是否已更改。 如果供稿發(fā)布者支持這些標頭,這將起作用,我強烈建議您使用這些標頭,因為這樣可以節(jié)省使用者的帶寬和處理能力。
那么它是如何工作的呢? 最初,當(dāng)將新的播客添加到Podcastpedia.org目錄時,我檢查供稿資源的標頭是否存在,如果存在,則將其存儲在數(shù)據(jù)庫中。 為此,我借助Apache Http Client對提要的URL執(zhí)行HTTP HEAD請求。 根據(jù)超文本傳輸??協(xié)議HTTP / 1.1 rfc2616 ,HTTP頭中包含的響應(yīng)HEAD請求的元信息應(yīng)與響應(yīng)GET請求發(fā)送的信息相同。
在以下各節(jié)中,我將介紹在升級到Apache Http Client的4.3.x版本之前和之后,代碼在Java中的實際外觀。
2.遷移到4.3.x版本
軟件依賴
要構(gòu)建我的項目,該項目現(xiàn)在可以在GitHub – Podcastpedia-batch上使用 ,我正在使用maven,因此在下面列出了Apache Http Client所需的依賴項:
2.1.1。 之前
Apache Http Client依賴項4.2.x
<!-- Apache Http client --> <dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.2.5</version> </dependency> <dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.2.4</version> </dependency>2.1.2。 后
Apache Http Client依賴項
<!-- Apache Http client --> <dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.3.5</version> </dependency> <dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.3.2</version> </dependency>Apache Http Client的HEAD請求
2.2.1。 v4.2.x之前
使用Apache HttpClient執(zhí)行HEAD請求的示例
private void setHeaderFieldAttributes(Podcast podcast) throws ClientProtocolException, IOException, DateParseException{HttpHead headMethod = null; headMethod = new HttpHead(podcast.getUrl());org.apache.http.client.HttpClient httpClient = new DefaultHttpClient(poolingClientConnectionManager);HttpParams params = httpClient.getParams();org.apache.http.params.HttpConnectionParams.setConnectionTimeout(params, 10000);org.apache.http.params.HttpConnectionParams.setSoTimeout(params, 10000);HttpResponse httpResponse = httpClient.execute(headMethod);int statusCode = httpResponse.getStatusLine().getStatusCode();if (statusCode != HttpStatus.SC_OK) {LOG.error("The introduced URL is not valid " + podcast.getUrl() + " : " + statusCode);}//set the new etag if existentorg.apache.http.Header eTagHeader = httpResponse.getLastHeader("etag");if(eTagHeader != null){podcast.setEtagHeaderField(eTagHeader.getValue());}//set the new "last modified" header field if existent org.apache.http.Header lastModifiedHeader= httpResponse.getLastHeader("last-modified");if(lastModifiedHeader != null) {podcast.setLastModifiedHeaderField(DateUtil.parseDate(lastModifiedHeader.getValue()));podcast.setLastModifiedHeaderFieldStr(lastModifiedHeader.getValue());} // Release the connection.headMethod.releaseConnection(); }如果您使用的是智能IDE,它將告訴您DefaultHttpClient , HttpParams和HttpConnectionParams已棄用。 如果您現(xiàn)在查看他們的Java文檔,將會得到替換建議,即使用HttpClientBuilder和org.apache.http.config提供的類。
因此,正如您將在下一節(jié)中看到的那樣,這正是我所做的。
2.2.2。 在v 4.3.x之后
帶有Apache Http Client v 4.3.x的HEAD請求示例
private void setHeaderFieldAttributes(Podcast podcast) throws ClientProtocolException, IOException, DateParseException{HttpHead headMethod = null; headMethod = new HttpHead(podcast.getUrl());RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(TIMEOUT * 1000).setConnectTimeout(TIMEOUT * 1000).build();CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setConnectionManager(poolingHttpClientConnectionManager).build();HttpResponse httpResponse = httpClient.execute(headMethod);int statusCode = httpResponse.getStatusLine().getStatusCode();if (statusCode != HttpStatus.SC_OK) {LOG.error("The introduced URL is not valid " + podcast.getUrl() + " : " + statusCode);}//set the new etag if existentHeader eTagHeader = httpResponse.getLastHeader("etag");if(eTagHeader != null){podcast.setEtagHeaderField(eTagHeader.getValue());}//set the new "last modified" header field if existent Header lastModifiedHeader= httpResponse.getLastHeader("last-modified");if(lastModifiedHeader != null) {podcast.setLastModifiedHeaderField(DateUtil.parseDate(lastModifiedHeader.getValue()));podcast.setLastModifiedHeaderFieldStr(lastModifiedHeader.getValue());} // Release the connection.headMethod.releaseConnection(); }注意:
- HttpClientBuilder如何用于構(gòu)建ClosableHttpClient [11-15行],這是HttpClient的基本實現(xiàn),該實現(xiàn)也實現(xiàn)了Closeable
- 以前版本的HttpParams已被org.apache.http.client.config.RequestConfig [第6-9行]取代,可以在其中設(shè)置套接字和連接超時。 稍后在構(gòu)建HttpClient時使用此配置(第13行)
剩下的代碼很簡單:
- HEAD請求被執(zhí)行(第17行)
- 如果存在,則eTag和last-modified標頭將保留。
- 最后,重置請求的內(nèi)部狀態(tài),使其可重用– headMethod.releaseConnection()
2.2.3。 從代理后面進行http呼叫
如果您位于代理后面,則可以通過在RequestConfig上設(shè)置org.apache.http.HttpHost代理主機來輕松配置HTTP調(diào)用:
代理后面的HTTP調(diào)用
HttpHost proxy = new HttpHost("xx.xx.xx.xx", 8080, "http"); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(TIMEOUT * 1000).setConnectTimeout(TIMEOUT * 1000).setProxy(proxy).build();資源資源
源代碼– GitHub
- podcastpedia-batch –將新的Podcast從文件添加到Podcast目錄的工作,使用帖子中提供的代碼來保留eTag和lastModified標頭; 它仍在進行中。 如果您有任何改進建議,請?zhí)岢鲆?
網(wǎng)頁
- 超文本傳輸??協(xié)議-HTTP / 1.1
- Maven倉庫
- HttpComponents客戶端
翻譯自: https://www.javacodegeeks.com/2014/08/how-to-use-the-new-apache-http-client-to-make-a-head-request.html
總結(jié)
以上是生活随笔為你收集整理的如何使用新的Apache Http Client发出HEAD请求的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 任务英语怎么说 任务英语解释
- 下一篇: 拼多多先用后付怎么关闭