2017-06-03 295 views
0

我正在使用Java和Apache HttpClient編寫播客下載程序。對於大多數RSS提要,它工作正常,但是這個失敗了「406 Not Acceptable」錯誤。Apache HttpClient接收「406 Not Acceptable」錯誤

的聯繫是http://sqrpt.com/feed/podcast/

我回來是響應:

HttpResponseProxy{HTTP/1.1 406 Not Acceptable [Server: nginx/1.12.0, Date: Sat, 03 Jun 2017 03:40:17 GMT, Content-Type: text/html; charset=iso-8859-1, Content-Length: 226, Connection: keep-alive] ResponseEntityProxy{[Content-Type: text/html; charset=iso-8859-1,Content-Length: 226,Chunked: false]}} 

我的Java代碼是:

HttpClient httpClient = HttpClients.custom().setUserAgent("Mozilla/5.0").build(); 
    HttpGet httpGet = new HttpGet(url); 
    httpGet.setHeader("Accept", "*/*"); 
    httpGet.setHeader("Accept-Encoding", "gzip, deflate, sdch"); 
    httpGet.setHeader("Accept-Language", "en-US,en;q=0.8"); 
    HttpResponse httpResponse = httpClient.execute(httpGet); 
    return httpResponse.getEntity(); 

我不知該怎麼調試這個東西。大多數帖子都說它的接受表頭有問題。我已經決定接受一切,但我仍然失敗。

當我在Chrome中訪問該網站時,我收到以下標題。它可能是導致問題的304返回碼嗎?

General 
Request URL:http://sqrpt.com/feed/podcast/ 
Request Method:GET 
Status Code:304 Not Modified 
Remote Address:192.185.32.200:80 
Referrer Policy:no-referrer-when-downgrade 

Response Headers 
view source 
Connection:keep-alive 
Date:Sat, 03 Jun 2017 03:46:27 GMT 
ETag:"c06764644cd3ec282be2807a54a3484c" 
Server:nginx/1.12.0 

Request Headers 
view source 
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
Accept-Encoding:gzip, deflate, sdch 
Accept-Language:en-US,en;q=0.8 
Cache-Control:max-age=0 
Connection:keep-alive 
Host:sqrpt.com 
If-Modified-Since:Fri, 02 Jun 2017 01:36:27 GMT 
If-None-Match:"c06764644cd3ec282be2807a54a3484c" 
Upgrade-Insecure-Requests:1 
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 

我還能試試嗎?

+0

我看到nginx在響應頭。我曾經遇到過這個問題,因爲解決方法是刷新nginx內容緩存,並且工作正常。我的406是因爲nginx和上游之間的衝突 – harshavmb

回答

1

它似乎檢查用戶代理。我得到它與以下用戶代理工作:

CloseableHttpClient httpClient = HttpClients.custom().setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36").build(); 
+0

您,先生,是正確的。那就是訣竅。謝謝。 – Todd