Server redirected too many times

 

我之前遇到過這種問題,有些網站是這樣的,他判斷cookie裏面是否有某個值,如果沒有就定向到另外一個頁面去把COOKIE的值設置了,然後再跳轉回來,但是如果我們通過HttpURLConnection去的話,cookie裏面一直都沒有那個值,所以他就一直在那裏跳來跳去,所以我們就需要一個CookieManager,在使用URLConnection之前設置它,只需要設置一次。

目前還不清楚這樣的設置是否會帶來更多的內存開銷。

如果有人願意研究,請告知。

 

System.setProperty("http.maxRedirects", "100");

It's apparently redirecting in an infinite loop because you don't maintain the user session. The session is usually backed by a cookie. You need to create a CookieManager before you use URLConnection.

// First set the default cookie manager.
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

// All the following subsequent URLConnections will use the same cookie manager.
URLConnection connection = new URL(url).openConnection();
// ...

connection = new URL(url).openConnection();
// ...

connection = new URL(url).openConnection();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章