1

I'm trying to import PAC file from URL and change Wifi proxy settings programmatically. I searched and found that it`s possible with:

ProxyInfo.buildPacProxy(Uri.parse("someurl")

Before asking this question I checked here and also all of this. The problem that I face is when I implement some of these solutions everything compiles well without exceptions, but when I check there are no proxy settings updated. This is my last code, but once again without success:

public void setWifiProxySettings5()
{
    //get the current wifi configuration
    WifiManager manager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    WifiConfiguration config = GetCurrentWifiConfiguration(manager);
    if(null == config)
        return;

    try
    {
        //linkProperties is no longer in WifiConfiguration
        Class proxyInfoClass = Class.forName("android.net.ProxyInfo");
        Class[] setHttpProxyParams = new Class[1];
        setHttpProxyParams[0] = proxyInfoClass;
        Class wifiConfigClass = Class.forName("android.net.wifi.WifiConfiguration");
        Method setHttpProxy = wifiConfigClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams);
        setHttpProxy.setAccessible(true);

        Class proxySettingsClass = Class.forName("android.net.IpConfiguration$ProxySettings");

        Class[] setProxySettingsParams = new Class[1];
        setProxySettingsParams[0] = proxySettingsClass;
        Method setProxySettings = wifiConfigClass.getDeclaredMethod("setProxySettings", setProxySettingsParams);
        setProxySettings.setAccessible(true);


        ProxyInfo pacInfo = ProxyInfo.buildPacProxy(Uri.parse("http://localhost/pac"));

        //pass the new object to setHttpProxy
        Object[] params_SetHttpProxy = new Object[1];
        params_SetHttpProxy[0] = pacInfo;
        setHttpProxy.invoke(config, params_SetHttpProxy);

        //pass the enum to setProxySettings
        Object[] params_setProxySettings = new Object[1];
        params_setProxySettings[0] = Enum.valueOf((Class<Enum>) proxySettingsClass, "STATIC");
        setProxySettings.invoke(config, params_setProxySettings);

        //save the settings
        manager.updateNetwork(config);
        manager.disconnect();
        manager.reconnect();
    }
    catch(Exception e)
    {
        Log.v("wifiProxy", e.toString());
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kristiyan Varbanov
  • 2,439
  • 2
  • 17
  • 37
  • Application can not set PAC file, please check Please look at https://developer.android.com/work/dpc/work-profile-on-managed-device and https://developer.android.com/reference/android/net/wifi/WifiConfiguration.html#setHttpProxy(android.net.ProxyInfo) – Andy Oct 23 '19 at 16:50
  • @Kristiyan have you found any solution? Can you share it? – Anant Shah Jan 22 '20 at 13:01

0 Answers0