HttpClient的重定向处理器 [英] Httpclient redirect handler

查看:202
本文介绍了HttpClient的重定向处理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用使用相对URL的Web服务器重定向的一些电话。这当然是不工作DefaultHttpClient因为它不把它当作一个相对URL。我尽可能实现RedirectHandler,企图搭上重定向,并添加在基本通话得到,但我不知道如何得到重定向的位置。

通过下面的方法我怎么去寻找在哪里我被重定向到?我找不到任何响应或上下文有什么,我需要的任何领域,我不知道还有什么地方看。

 公共URI getLocationURI(Htt的presponse响应,HttpContext的背景下)
 

解决方案

我的解决办法是阅读的位置标头和跟随他们。帮我:

 如果(状态code!= HttpStatus.SC_OK){
    标题[]头= response.getHeaders(位置);

    如果(头= NULL和放大器;!&安培;!headers.length = 0){
        字符串的newURL =标题[headers.length  -  1] .getValue();
        //再次调用相同的下载方法与新的URL
        返回downloadBitmap(的newURL);
    } 其他 {
        返回null;
    }
}
 

更在我的岗位 - 按照302 AndroidHttpClient

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) 

解决方案

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

这篇关于HttpClient的重定向处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆