【问题标题】:Getting WiFi proxy settings in Android在 Android 中获取 WiFi 代理设置
【发布时间】:2023-03-30 23:05:02
【问题描述】:

我正在尝试读取 WIFI 代理设置

  • 代理主机
  • 代理端口
  • 代理用户(身份验证)
  • 代理密码(认证)

来自 android 版本 2.X.X – 4.X.X 的设备没有任何成功。

调用:

String proxy = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.HTTP_PROXY);

总是返回 null。

我还添加到我的 android 清单中:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

它仍然返回 null。

也试过了:

android.net.Proxy. getHost(Context ctx) – which is deprecated – returns the IP
android.net.Proxy. getPortt(Context ctx) – which is deprecated – returns always -1.

Java 调用:

System.getProperty("http.proxyHost");
System.getProperty("http.proxyCall");

同样返回 null。

是否有工作代码可以检索所有这些设置或至少部分从所有 android 版本的设备中检索?

【问题讨论】:

  • 您好 user1222022。你的问题解决了吗?

标签: android proxy wifi


【解决方案1】:

我找到了这个项目:Android Proxy Library 它提供了向后兼容的方式来查询代理设置以及为旧版本 Android 上的 WebView 设置它们。

    // Grab Proxy settings in a backwards compatible manner
    ProxyConfiguration proxyConfig = ProxySettings.getCurrentHttpProxyConfiguration( context );

    // Set Proxy for WebViews on older versions of Android
    ProxyUtils.setWebViewProxy( getActivity().getApplicationContext() );

但是,您需要了解有关在 WiFi AP 上设置的代理设置的一些信息。由于 WiFi 特定代理设置直到 3.1 才在 Android 中实现,所有暴露该功能的 3.1 之前的设备都在使用某种自定义黑客。它们不以任何标准方式工作。因此,像这样的库将无法从其中一个黑客中获取任何代理集。

然而,在 pre-3.1 中有一个系统范围的代理,这种库 抓取。当然,Android 认为不提供任何官方方式来修改此设置是合适的。但是 Play Store 上有一些应用程序可以让你这样做,这就是我正在使用的那个:Proxy Settings 它运行良好,设置系统代理并允许你通过这个库获取它,甚至更简单的方法,例如查询 JVM 代理设置。

我最终没有使用 APL,而是使用了一个更简单的实现:

    private static final boolean IS_ICS_OR_LATER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;

    ...

    String proxyAddress;
    int proxyPort;

    if( IS_ICS_OR_LATER )
    {
        proxyAddress = System.getProperty( "http.proxyHost" );

        String portStr = System.getProperty( "http.proxyPort" );
        proxyPort = Integer.parseInt( ( portStr != null ? portStr : "-1" ) );
    }
    else
    {
        proxyAddress = android.net.Proxy.getHost( context );
        proxyPort = android.net.Proxy.getPort( context );
    }

【讨论】:

  • 您好,我是创建 Android 代理库的开发人员。关于 APL 的更多信息可以在项目的官方网站上找到:http://www.android-proxy.com
  • @Tirtha 当然,您必须针对更高版本的操作系统,最低版本设置得更低。我们已经对其进行了测试并将其使用回 API 级别 7 (2.1),并且仍在 API 8 和 9 (2.2 和 2.3) 上积极使用它,因此它确实有效。
  • 我正在使用这段确切的代码,我的清单是这样的 我正在 Android 2.2 上编译但它仍然不起作用。
  • 您知道是否可以通过编程方式知道 wifi 是否有诸如 whatsapp instagram 等服务被阻止的情况?
【解决方案2】:

这是我正在使用的:

public static String[] getUserProxy(Context context)
{
    Method method = null;
    try
    {
      method = ConnectivityManager.class.getMethod("getProxy");
    }
    catch (NoSuchMethodException e)
    {
      // Normal situation for pre-ICS devices
      return null;
    }
    catch (Exception e)
    {
      return null;
    }

    try
    {
      ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      Object pp = method.invoke(connectivityManager);
      if (pp == null)
        return null;

      return getUserProxy(pp);
    }
    catch (Exception e)
    {
      return null;
    }
  }


private static String[] getUserProxy(Object pp) throws Exception
{
    String[] userProxy = new String[3];

    String className = "android.net.ProxyProperties";
    Class<?> c = Class.forName(className);
    Method method;

    method = c.getMethod("getHost");
    userProxy[0] = (String) method.invoke(pp);

    method = c.getMethod("getPort");
    userProxy[1] = String.valueOf((Integer) method.invoke(pp));


    method = c.getMethod("getExclusionList");
    userProxy[2] = (String) method.invoke(pp);

    if (userProxy[0] != null)
      return userProxy;
    else
      return null;
}

【讨论】:

  • 为我工作...很好地使用 API
  • Android Marshmallow 的等价物是什么?
【解决方案3】:

以下是获取代理详细信息的代码 sn-p

public static String getProxyDetails(Context context) {
        String proxyAddress = new String();
        try {
            if (IsPreIcs()) {
                proxyAddress = android.net.Proxy.getHost(context);
                if (proxyAddress == null || proxyAddress.equals("")) {
                    return proxyAddress;
                }
                proxyAddress += ":" + android.net.Proxy.getPort(context);
            } else {
                proxyAddress = System.getProperty("http.proxyHost");
                proxyAddress += ":" + System.getProperty("http.proxyPort");
            }
        } catch (Exception ex) {
            //ignore
        }
        return proxyAddress;
    }

如果检测到异常或未检测到代理,它将返回 enmpty;

【讨论】:

  • IsPreIcs 是什么?顺便说一句,您是否知道以编程方式我是否可以知道 wifi 是否有诸如whatsapp instagram 等服务被阻止?
【解决方案4】:
private fun getUserProxy(context: Context): Data {
    return try {
        val declaredField = WifiConfiguration::class.java.getDeclaredField("mIpConfiguration")
        declaredField.isAccessible = true

        val data =
            (context.applicationContext.getSystemService(Context.WIFI_SERVICE) as? WifiManager)
                ?.configuredNetworks
                ?.asSequence()
                ?.mapNotNull {
                    try {
                        declaredField.get(it)
                    } catch (e: Exception) {
                        e.printStackTrace()
                        null
                    }
                }
                ?.mapNotNull {
                    try {
                        (it.javaClass.getDeclaredField("httpProxy").get(it) as? ProxyInfo)
                    } catch (e: Exception) {
                        e.printStackTrace()
                        null
                    }
                }
                ?.find { !it.host.isNullOrEmpty() }
                ?.let { Data(it.host ?: "", it.port.toString()) }
                ?: Data()

        declaredField.isAccessible = false
        return data
    } catch (e: Exception) {
        e.printStackTrace()
        Data()
    }
}

data class Data(
    val host: String = "",
    val port: String = ""
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-26
    • 2014-03-20
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多