我的Google HTTP Client Library for Java(1.22.0)出現問題。 這是我的代碼用於Java的Google HTTP客戶端庫拋出HttpResponseException:301永久移動
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
和下一個請求方法得到是/ogl/2502758/pakowacz%20-%20mile%20widziane%20panie
。 在其他軟件和庫中,一切正常(google chrome瀏覽器,postman - 插件到chrome,JSOUP - java庫)。
有沒有人有一個想法如何解決這個問題?
非常感謝你提供了很好的技術信息。在這種情況下,我可以做些什麼嗎?我明白,我使用的圖書館(谷歌HTTP客戶端)工作正常,但遠程服務器沒有。如果我收到30x,我有一個想法可以使用另一個庫。你怎麼看待這件事? –
在此庫的源代碼中查找後,它看起來好像有一個實例變量'followRedirects',它可以通過類'HttpRequest'的實例方法'setFollowRedirects(boolean)'設置(您在代碼中)。所以也許將它設置爲false,然後通過獲取響應的'Location'頭來跟蹤重定向。 –
我之前設置了這個(請參閱我的主要帖子) –