dns劫持 - Android用XUtils下载apk文件被运营商劫持,如何处理.
PHP中文网
PHP中文网 2017-04-17 17:48:40
0
4
655

https://support.qiniu.com/hc/...
七牛提供的解决方法,但是项目中应用次方法局限性太大.
遂问还有其他的解决方法吗?

//自己解决了
自己替换掉xutils的重定向handler

public class NoHttpRedirectHandler implements HttpRedirectHandler {
    private String url;

    public NoHttpRedirectHandler(String url) {
        this.url=url;
    }

    public HttpRequestBase getDirectRequest(HttpResponse response) {
        if(response.containsHeader("Location")) {
            HttpGet request = new HttpGet(url);
            MGLog.e("upgrade","可恶,但是已经阻止了重定向!");
            if(response.containsHeader("Set-Cookie")) {
                String cookie = response.getFirstHeader("Set-Cookie").getValue();
                request.addHeader("Cookie", cookie);
            }
            return request;
        } else {
            return null;
        }
    }
}

实际使用:

HttpManagerX.getHttpUtils().configHttpRedirectHandler(new NoHttpRedirectHandler(url)).download(url, target, true, new
                RequestCallBack<File>(){...}

当被重定向到其他的url时,我们把重定向的url替换为我们原来的.

PHP中文网
PHP中文网

认证0级讲师

reply all(4)
阿神

Replace the redirection handler of xutils yourself

public class NoHttpRedirectHandler implements HttpRedirectHandler {
    private String url;

    public NoHttpRedirectHandler(String url) {
        this.url=url;
    }

    public HttpRequestBase getDirectRequest(HttpResponse response) {
        if(response.containsHeader("Location")) {
            HttpGet request = new HttpGet(url);
            MGLog.e("upgrade","可恶,但是已经阻止了重定向!");
            if(response.containsHeader("Set-Cookie")) {
                String cookie = response.getFirstHeader("Set-Cookie").getValue();
                request.addHeader("Cookie", cookie);
            }
            return request;
        } else {
            return null;
        }
    }
}

Actual use:

HttpManagerX.getHttpUtils().configHttpRedirectHandler(new NoHttpRedirectHandler(url)).download(url, target, true, new
                RequestCallBack<File>(){...}

When being redirected to other URLs, we replace the redirected URL with our original one.

PHPzhong

Go to https

伊谢尔伦

http has this kind of flaw. If you use https encryption, you can get a self-signed certificate and use it first.

巴扎黑

https, although if you directly disable redirection, this problem may appear to be solved, but if your download link itself has a redirection action, you will not be able to download it

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template