2011-12-29 71 views
0

我一直試圖在Java代碼中處理重定向(302),現在我完成了我的代碼。我遇到了另一個問題,在登錄後點擊我註銷的任何鏈接。所以我通過wireshark檢查了我的TCP流,發現缺少幾個HeaderRequests。實現我的代碼後,HTTP頭如下:將HttpMethod的內容複製到另一個HttpMethod

GET /index.php/ HTTP/1.1 
Host: 10.28.161.31 
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 CentOS/3.6-3.el5.centos Firefox/3.6.24 
Cookie: PHPSESSID=d488eea5e85afc8ec526c1a749e7ab20; path=/ 
Referrer: http://10.28.161.31 
Cookie: $Version=0; PHPSESSID=d488eea5e85afc8ec526c1a749e7ab20; $Path=/ ??? 

和原始HTTP頭如下:

GET/HTTP/1.1 
Host: 10.28.161.31 
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 CentOS/3.6-3.el5.centos Firefox/3.6.24 
Referer: http://10.28.161.31/index.php 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-us,en;q=0.5 
Accept-Encoding: gzip,deflate 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 
Keep-Alive: 115 
Connection: keep-alive 
Cookie: PHPSESSID=978ee1e3b3696743c5c8f507a2ec7212 

據我的觀察,我沒有正確複製的標題的內容,這就是爲什麼它正在快速註銷。所以我的問題是,如何將HttpMethod的完整內容複製到另一個HttpMethod?如果任何人可以提供代碼片段,或者示例/教程會很棒,或者如果任何人能夠提醒我我做錯事的地方,那將是可觀的。

我的實現就在這裏:

private HttpMethod loadHttp302Request(HttpMethod method, HttpClient client, 
       int status, String urlString) throws HttpException, IOException { 
    if (status!=302) 
     return null; 
    String[] url = urlString.split("/");   
    HttpMethod theMethod = new GetMethod(urlString + method.getResponseHeader("Location").getValue()); 
    theMethod.setRequestHeader("Cookie", method.getResponseHeader("Set-Cookie") 
        .getValue()); 
    theMethod.setRequestHeader("Referrer",url[0]+"//"+url[2]); 
    int _status = client.executeMethod(theMethod); 
    return theMethod; 
} 

回答

0

HttpClient的可以自動,如果你設置的策略處理重定向。使用示例Httpclient 4, error 302. How to redirect?

+0

請仔細閱讀這篇文章,究竟如何做一個翻譯黑/白HttpClient和DefaultHttpClient? – Ahmed 2011-12-30 00:47:23

+0

不適用於我.. – Ahmed 2011-12-30 19:34:44

相關問題