Google Java 的 HTTP 客户端库抛出 HttpResponseException:301 永久移动

Google HTTP Client Library for Java throws HttpResponseException: 301 Moved Permanently

我对 Java (1.22.0) 的 Google HTTP 客户端库有疑问。 这是我的代码

String url = "http://gazetapraca.pl/ogl/2502758";
GenericUrl genericUrl = new GenericUrl(url);
ApacheHttpTransport apacheHttpTransport = new ApacheHttpTransport();
HttpRequest httpRequest = apacheHttpTransport.createRequestFactory().buildGetRequest(genericUrl);
httpRequest.setFollowRedirects(true);
HttpResponse httpResponse = httpRequest.execute();

和 httpRequest.execute() 抛出

     com.google.api.client.http.HttpResponseException: 301 Moved Permanently

以下是 Wireshark 的关注

GET /ogl/2502758 HTTP/1.1
Accept-Encoding: gzip
User-Agent: Google-HTTP-Java-Client/1.22.0 (gzip)
Host: gazetapraca.pl
Connection: Keep-Alive

HTTP/1.1 301 Moved Permanently
Date: Sat, 26 Nov 2016 22:15:52 GMT
Server: Apache
Location: /ogl/2502758/pakowacz+-+mile+widziane+panie
Content-Length: 0
Set-Cookie: JSESSIONID_JOBS=2f1TffY6JYcb6zvBSrQ72fds7rfdsSnHM3sefw6D31Lfr434bnkDmdLQJLvLFZ6zkYBF!-12116034235597; path=/; HttpOnly
Content-Language: pl
P3P: CP="NOI DSP COR NID PSAo OUR IND"
Vary: User-Agent
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive

GET /ogl/2502758/pakowacz%20-%20mile%20widziane%20panie HTTP/1.1
Accept-Encoding: gzip
User-Agent: Google-HTTP-Java-Client/1.22.0 (gzip)
Host: gazetapraca.pl
Connection: Keep-Alive
Cookie: JSESSIONID_JOBS=2f1TffY6JYcb6zvBSrQ72fds7rfdsSnHM3sefw6D31Lfr434bnkDmdLQJLvLFZ6zkYBF!-12116034235597

HTTP/1.1 301 Moved Permanently
Date: Sat, 26 Nov 2016 22:15:52 GMT
Server: Apache
Location: /ogl/2502758/pakowacz+-+mile+widziane+panie
Content-Length: 0
Content-Language: pl
P3P: CP="NOI DSP COR NID PSAo OUR IND"
Vary: User-Agent
Keep-Alive: timeout=2, max=99
Connection: Keep-Alive

重复几次。 也许问题出在 url,因为 location/ogl/2502758/pakowacz+-+mile+widziane+panie 而下一个请求方法 get 是 /ogl/2502758/pakowacz%20-%20mile%20widziane%20panie。 在其他软件和库中,一切正常(google chrome 浏览器、邮递员 - chrome 的插件、JSOUP - java 库)。

有人知道如何解决这个问题吗?

这不是您图书馆的错。

要了解出现此问题的原因,我们必须首先了解与您的问题相关的 "error" 消息:

com.google.api.client.http.HttpResponseException: 301 Moved Permanently

那么,这是什么意思?那么,错误消息的最后一部分,描述为“301 永久移动”。它指的是 HTTP 状态代码。 HTTP 状态代码指示特定请求的结果是什么。在这种情况下,状态代码为 301,根据 RFC 协议,这意味着:

The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs.

因此,这意味着您正在使用的 URL 不再有效,您必须使用 Location 回复给您的新 URL header。现在,您正在使用的库似乎足够聪明,可以检测到这一点,并初始化对新 URL 的新请求。太好了,但是您正在使用的库错误地转义了 Location header 提供的 url,并将其用于新请求(转向 /ogl/2502758/pakowacz+-+mile+widziane+panie进入 /ogl/2502758/pakowacz%20-%20mile%20widziane%20panie),并且接收此请求的服务器识别出这些路径不相同(即使它们应该相同。因此,服务器发送另一个 301 响应,告诉客户端(在本例中为库)使用 un-escaped URL 而不是转义的,即使它们应该是相同的。

现在,为什么您的图书馆要这样做?事实证明,根据 RFC 规范,“+”字符是为 URI 保留的。这意味着该字符以及其他字符仅用于 URI 中的预期目的。因此,在 URI 中包含“+”字符是不标准的,除非它用于非常特定的目的,但看起来并非如此。

所以,这一切都意味着你不能为这个错误责怪图书馆,你只能责怪开发这个网站的人。

这在您的浏览器和其他地方起作用的原因是因为这些客户端似乎没有在将请求发送到服务器之前为您转义 URL。