2010-11-09 39 views
1

我正在嘗試調用一些使用相對URL重定向進行某些調用的Web服務器。這當然不適用於DefaultHttpClient,因爲它不會將其視爲相對URL。我已經儘可能的實現了一個RedirectHandler來嘗試捕獲重定向並添加基本調用,但我無法弄清楚如何獲取重定向的位置。Httpclient重定向處理程序

用下面的方法我該如何去找出我被重定向到的位置?我無法找到任何需要回應或上下文的字段,我也不知道還有哪些地方需要注意。

public URI getLocationURI(HttpResponse response, HttpContext context) 

回答

0

看一看這裏:HttpClient 4 - how to capture last redirect URL

我會嘗試getStatusLine()的開始。

+3

我看到了那個帖子。這個方法似乎是要求在完成請求後發現重定向。當我在重定向期間嘗試使用該方法時,我只需輸入我輸入的初始URL。 getStatusLine()只是給我200 OK – skorulis 2010-11-09 22:27:49

0

我的解決方案是讀取位置標題並遵循它們。這幫助了我:

在我的崗位
if (statusCode != HttpStatus.SC_OK) { 
    Header[] headers = response.getHeaders("Location"); 

    if (headers != null && headers.length != 0) { 
     String newUrl = headers[headers.length - 1].getValue(); 
     // call again the same downloading method with new URL 
     return downloadBitmap(newUrl); 
    } else { 
     return null; 
    } 
} 

更多 - Follow 302 redirects with AndroidHttpClient

相關問題