简体   繁体   English

排在代理服务器后面

[英]Volley behind a proxy server

I am new to Volley Networking Library (of Android). 我是Volley Networking Library(Android版)的新手。 I have observed that the Request function takes URL as the parameter instead of server name and port. 我观察到Request函数将URL作为参数而不是服务器名称和端口。 Is there any way for making Volley request to go through a Proxy Server of my choice if I mention the server name and the port? 如果我提到服务器名称和端口,有没有办法让Volley请求通过我选择的代理服务器?

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

I know that we can make use of server and port info while building the URL but is there a way other than this to make sure that the requests go through a Proxy mentioned by us ? 我知道我们可以在构建URL时使用服务器和端口信息,但除此之外还有其他方法可以确保请求通过我们提到的代理吗?

For example: How do I make HttpURLConnection use a proxy? 例如: 如何使HttpURLConnection使用代理? Here is a method to ensure that HttpURLConnection uses a proxy. 这是一种确保HttpURLConnection使用代理的方法。 I am looking for similar answers for Volley. 我正在为Volley寻找类似的答案。

Volley doesn't offer any direct methods to set proxy but there is a way. Volley没有提供任何直接设置代理的方法,但有一种方法。

Create a custom class extending HurlStack, say ProxiedHurlStack 创建一个扩展HurlStack的自定义类,比如ProxiedHurlStack

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

import com.android.volley.toolbox.HurlStack;

public class ProxiedHurlStack extends HurlStack {

    @Override
    protected HttpURLConnection createConnection(URL url) throws IOException {

        // Start the connection by specifying a proxy server
        Proxy proxy = new Proxy(Proxy.Type.HTTP,
                InetSocketAddress.createUnresolved("192.168.1.11", 8118));//the proxy server(Can be your laptop ip or company proxy)
        HttpURLConnection returnThis = (HttpURLConnection) url
                .openConnection(proxy);

        return returnThis;
    }
}

Now initialize your queue using: 现在使用以下命令初始化队列:

mRequestQueue = Volley.newRequestQueue(context, new ProxiedHurlStack());

Courtesy: http://d.hatena.ne.jp/esmasui/20130613/1371126800 礼貌: http//d.hatena.ne.jp/esmasui/20130613/1371126800

If users set up a proxy at the system level (eg proxy host for a wifi connection), then you won't explicitly know that inside your app. 如果用户在系统级别设置代理(例如,用于wifi连接的代理主机),那么您将不会在应用程序内明确知道。 To get around this, I also extended HurlStack to use the first provisioned proxy: 为了解决这个问题,我还扩展了HurlStack以使用第一个配置的代理:

public class ProxyHurlStack extends HurlStack {

    @Override
    protected HttpURLConnection createConnection(URL url) throws IOException {
        final HttpURLConnection urlConnection;
        Proxy proxy = null;
        try {
            proxy = ProxySelector.getDefault().select(url.toURI()).get(0);
        } catch (Exception e) {
            Ln.e(e, e.getMessage());
        }
        if (proxy == null) {
            urlConnection = (HttpURLConnection) url.openConnection();
        } else {
            urlConnection = (HttpURLConnection) url.openConnection(proxy);
        }
        return urlConnection;
    }
}

Justin Lee has the best Answer for that question. Justin Lee对这个问题有最好的答案。 Firstly I was afraid about ProxySelector returning more than one Proxy definition, but, actually, it returns only one, even though the return type is a List. 首先,我担心ProxySelector返回多个Proxy定义,但实际上,它只返回一个,即使返回类型是List。

@Override public List<Proxy> select(URI uri) {
    return Collections.singletonList(selectOneProxy(uri));
}

private Proxy selectOneProxy(URI uri) {[...]

Secondly I think that using the user system wild set up would be a good practice. 其次我认为使用用户系统进行疯狂设置将是一个很好的做法。 For the sake of usability and dynamic proxy configuration. 为了可用性和动态代理配置。 See here for more information about the select(java.net.URI) method. 有关select(java.net.URI)方法的更多信息,请参见此处

Oh, by the way, if I was you, I would see HurlStack implementation of the createConnection(java.net.Url) first. 哦,顺便说一句,如果我是你,我会首先看到createConnection(java.net.Url) HurlStack实现。 Here it is: 这里是:

/**
 * Create an {@link HttpURLConnection} for the specified {@code url}.
 */
protected HttpURLConnection createConnection(URL url) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    // Workaround for the M release HttpURLConnection not observing the
    // HttpURLConnection.setFollowRedirects() property.
    // https://code.google.com/p/android/issues/detail?id=194495
    connection.setInstanceFollowRedirects(HttpURLConnection.getFollowRedirects());

    return connection;
}

Look those comments... 看那些评论......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM