Httpclient 重定向处理程序

发布于 09-30 22:53 字数 304 浏览 10 评论 0原文

我正在尝试调用对某些调用使用相对 URL 重定向的 Web 服务器。这当然不适用于 DefaultHttpClient,因为它不将其视为相对 URL。我已经实现了 RedirectHandler 来尝试捕获重定向并添加到基本调用中,但我无法弄清楚如何获取重定向的位置。

使用以下方法如何找出我被重定向到的位置?我在响应或上下文中找不到任何包含我需要的字段,并且我不知道还能在哪里查找。

public URI getLocationURI(HttpResponse response, HttpContext context) 

I'm trying to call a web server that is using relative URL redirects for some of the calls. This of course isn't working with DefaultHttpClient as it isn't treating it as a relative URL. I've gotten as far as implementing a RedirectHandler in an attempt to catch the redirect and add in the base call but I can't work out how to get the location of the redirect.

With the following method how do I go about finding out where I am being redirected to? I can't find any fields on either response or context that have what I need and I don't know where else to look.

public URI getLocationURI(HttpResponse response, HttpContext context) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初相遇2024-10-07 22:53:44

看看这里:HttpClient 4 - 如何捕获最后一个重定向 URL

我会尝试使用 getStatusLine() 开始。

Have a look here: HttpClient 4 - how to capture last redirect URL

I would try getStatusLine() for start.

¢好甜2024-10-07 22:53:44

我的解决方案是读取位置标题并遵循它们。这对我有帮助:

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;
    }
}

更多内容在我的帖子中 - 使用 AndroidHttpClient 进行 302 重定向< /a>

My solution is to read location headers and follow them. This helped me:

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;
    }
}

More in my post - Follow 302 redirects with AndroidHttpClient

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文