google / volley

https://google.github.io/volley
Apache License 2.0
3.38k stars 754 forks source link

Use proxy with Volley #429

Closed alex2844 closed 2 years ago

alex2844 commented 2 years ago

Good afternoon. Tell me how to login to the proxy

import android.app.AlertDialog;
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.Volley;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
public class proxyVolley {
    private static RequestQueue mRequestQueue;
    private static final String URL = "https://api.ipify.org/?format=json";
    public static String proxy = ""; // user:pass@host:port
    public static class ProxiedHurlStack extends HurlStack {
        @Override
        protected HttpURLConnection createConnection(URL url) throws IOException {
            String host_ = proxy.replaceAll("^(.*?)@", "").replaceAll("^(.*?):(.*?)$", "$1");
            String port_ = proxy.replaceAll("^(.*?)@", "").replaceAll("^(.*?):(.*?)$", "$2");
            String user_ = proxy.replaceAll("^(.*?):(.*?)@(.*?)$", "$1");
            String pass_ = proxy.replaceAll("^(.*?):(.*?)@(.*?)$", "$2");
            return (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(host_, Integer.parseInt(port_))));
        }
    }
    public static void showIpAddress(Context context) {
        if (mRequestQueue == null)
                mRequestQueue = Volley.newRequestQueue(context, (!proxy.isEmpty() ? new ProxiedHurlStack() : null));
        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        mRequestQueue.add(new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                alert.setTitle("Ok");
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    alert.setMessage("My IP Address is = " + jsonObject.getString("ip"));
                } catch (JSONException e) {
                    alert.setMessage("JSON Exception = " + e.getMessage());
                    e.printStackTrace();
                }
                alert.show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                alert.setTitle("Error").setMessage("Volley error when get IP Address = " + error.getMessage()).show();
            }
        }));
    }
}

I get the error: "failed to authenticate with proxy"

jpd236 commented 2 years ago

Hi there,

This doesn't seem like a Volley question so much as it is one about HttpURLConnection, since Volley isn't at all involved in the proxy connection process. Presumably you'd have the same problem when using HttpURLConnection directly, since Volley isn't doing anything special here.

I'd recommend you remove Volley from the equation by using HttpURLConnection directly and figuring out why that's not working. If you're able to get things working directly, but for some reason Volley is causing problems, then feel free to reopen this with more details about the version that works and the one that doesn't. Otherwise, I'm closing this out as I do not believe there is a Volley issue here, and there are more suitable general help venues for how to use HttpURLConnections with proxy servers.