Volley behind a proxy server
Asked Answered
B

3

6

I am new to Volley Networking Library (of Android). I have observed that the Request function takes URL as the parameter instead of server name and port. 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?

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 ?

For example: How do I make HttpURLConnection use a proxy? Here is a method to ensure that HttpURLConnection uses a proxy. I am looking for similar answers for Volley.

Bowser answered 28/5, 2014 at 14:27 Comment(0)
P
14

Volley doesn't offer any direct methods to set proxy but there is a way.

Create a custom class extending HurlStack, say 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

Perturbation answered 17/3, 2015 at 22:17 Comment(1)
This method would set up a RequestQueue with a proxy using the URL object pointing to one specific url. What if you want to add new Requests to the queue for other url's with the same proxy? Would you need a new Queue for each url?Adiathermancy
A
5

If users set up a proxy at the system level (e.g. proxy host for a wifi connection), then you won't explicitly know that inside your app. To get around this, I also extended HurlStack to use the first provisioned proxy:

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;
    }
}
Aiaia answered 7/11, 2015 at 18:41 Comment(0)
A
1

Justin Lee has the best Answer for that question. 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.

@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.

Oh, by the way, if I was you, I would see HurlStack implementation of the createConnection(java.net.Url) first. 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...

Anhanhalt answered 5/5, 2016 at 16:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.