Proxy Hall

I am new to Volley Networking Library (android). I noticed that the Request function uses the URL as a parameter instead of the server name and port. Is there a way to make a Volley request to a proxy server of my choice if I mention the server name and port?

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

I know that we can use the server and port information when creating the URL, but is there a way, besides this, to make sure the requests go through the proxy server we mentioned?

For an example: How to make HttpURLConnection use a proxy? The following is a method to ensure that the HttpURLConnection uses a proxy server. I am looking for similar answers for Volley.

+3
url proxy android-volley
May 28 '14 at 14:27
source share
3 answers

Volleyball does not offer direct methods for installing a proxy server, but there is a way.

Create your own HurlStack extension class, 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 the queue using:

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

Provided by: http://d.hatena.ne.jp/esmasui/20130613/1371126800

+9
Mar 17 '15 at
source share

If users set up proxies at the system level (for example, a proxy host for Wi-Fi), you will not explicitly know this inside your application. To get around this, I also added HurlStack to use the first proxy provided:

 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; } } 
+2
Nov 07 '15 at 18:41
source share

Justin Lee has the best answer to this question. First, I was afraid that ProxySelector would return more than one proxy definition, but in fact it only returns one, although the return type is List.

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

Secondly, I think that using a wild custom system setup would be good practice. For ease of use and dynamic proxy configuration. See here for more information on the select(java.net.URI) method select(java.net.URI) .

Oh, by the way, if I were you, I would first see the createConnection(java.net.Url) implementation in HurlStack. There he 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; } 

Check out these comments ...

0
May 05 '16 at 16:59
source share



All Articles