2010-11-09 89 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