Apache HttpClient接收“406 Not Acceptable”错误

时间:2017-06-03 19:19:02

标签: java http rss apache-httpclient-4.x

我正在使用Java和Apache HttpClient编写播客下载程序。它适用于大多数RSS提要,但是这个提示失败并显示“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

我还能尝试什么?

1 个答案:

答案 0 :(得分:1)

它似乎检查了User-Agent。我让它与以下用户代理一起工作:

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();