Android中的手动代理通过反射

Android中的手动代理通过反射,第1张

概述我试图在 android中通过反射设置ssid,proxy,ipsetting并成功通过反射在android中通过ssid和简短的ip设置,我的问题是我想通过反射程序设置代理设置和大部分代码我goggled说实现STATIC和NONE选项,但我的代理选项为NONE和MANUAL的设备是否相同?下面是我的代理plz的代码建议我应该更改为手动代理实现的工作: public static void se 我试图在 android中通过反射设置ssID,proxy,ipsetting并成功通过反射在androID中通过ssID和简短的ip设置,我的问题是我想通过反射程序设置代理设置和大部分代码我goggled说实现STATIC和NONE选项,但我的代理选项为NONE和MANUAL的设备是否相同?下面是我的代理plz的代码建议我应该更改为手动代理实现的工作:

public static voID setWifiProxySettings(WifiConfiguration config,WifiSetting wifiSetting) {        try {            Object linkPropertIEs = getFIEld(config,"linkPropertIEs");            if (null == linkPropertIEs)                return;            Class proxyPropertIEsClass = Class                    .forname("androID.net.ProxyPropertIEs");            Class[] sethttpProxyParams = new Class[1];            sethttpProxyParams[0] = proxyPropertIEsClass;            Class lpClass = Class.forname("androID.net.linkPropertIEs");            Method sethttpProxy = lpClass.getDeclaredMethod("sethttpProxy",sethttpProxyParams);            sethttpProxy.setAccessible(true);            Class[] proxyPropertIEsCtorParamTypes = new Class[3];            proxyPropertIEsCtorParamTypes[0] = String.class;            proxyPropertIEsCtorParamTypes[1] = int.class;            proxyPropertIEsCtorParamTypes[2] = String.class;            Constructor proxyPropertIEsCtor = proxyPropertIEsClass                    .getConstructor(proxyPropertIEsCtorParamTypes);            Object[] proxyPropertIEsCtorParams = new Object[3];            URL proxyUrl = new URL(wifiSetting.getProxyHostname());            proxyPropertIEsCtorParams[0] = proxyUrl.getHost();            proxyPropertIEsCtorParams[1] = proxyUrl.getPort();            proxyPropertIEsCtorParams[2] = null;            Object proxySettings = proxyPropertIEsCtor                    .newInstance(proxyPropertIEsCtorParams);            Object[] params = new Object[1];            params[0] = proxySettings;            sethttpProxy.invoke(linkPropertIEs,params);            setProxySettings("STATIC",config);        } catch (Exception e) {            e.printstacktrace();        }    }    public static voID setProxySettings(String assign,WifiConfiguration wifiConf)            throws SecurityException,IllegalArgumentException,NoSuchFIEldException,illegalaccessexception {        setEnumFIEld(wifiConf,assign,"proxySettings");    }
解决方法 我通过反思在androID下面写了代理APIs:

/**         * This API is used for setting IP And Proxy Setting using below         * supporting methods         *          * @param wifiSetting         * @param wifiConf         */        public static voID setWifiSettings(WifiSetting wifiSetting,WifiConfiguration wifiConf) {            // check if ip setting is static for custom ip setting            // configration            if ("STATIC".equals(wifiSetting.getIpSetting())) {                setIpSettings(wifiSetting,wifiConf);            }            // if proxy is enabled set its custom proxy settings            if (wifiSetting.getIsProxyEnabled() == true) {                setWifiProxySettings(wifiSetting,wifiConf);            }        }        /**         * This API is used for setting IP         *          * @param wifiSetting         * @param wifiConf         */        private static voID setIpSettings(WifiSetting wifiSetting,WifiConfiguration wifiConf) {            try {                setEnumFIEld(wifiConf,"STATIC","ipAssignment");                setIpAddress(wifiSetting.getIpAddress(),wifiSetting.getNetworkPrefixLength(),wifiConf);                setGateway(wifiSetting.getGateway(),wifiConf);                setDNS(wifiSetting,wifiConf);            } catch (Exception e) {                e.printstacktrace();            }        }        /**         * This API is used for setting IpAddress in wifi settings         *          * @param ipAddres         * @param prefixLength         */        private static voID setIpAddress(String ipAddress,int networkPrefixLength,WifiConfiguration wifiConf)                throws SecurityException,illegalaccessexception,NoSuchMethodException,ClassNotFoundException,InstantiationException,InvocationTargetException {            try {                if (TextUtils.isEmpty(ipAddress)) {                    throw new IllegalArgumentException(                            "required argument can not be blank.");                }                InetAddress inetAddr = null;                Class networkUtils = Class.forname("androID.net.NetworkUtils");                Method numericToInetAddress = networkUtils.getDeclaredMethod(                        "numericToInetAddress",ipAddress.getClass());                inetAddr = (InetAddress) numericToInetAddress.invoke(null,ipAddress);                if (networkPrefixLength < 0 || networkPrefixLength > 32) {                    throw new IllegalArgumentException(                            "invalID networkPrefixLength parameter");                }                Object linkPropertIEs = getFIEldValue(wifiConf.getClass(),wifiConf,"linkPropertIEs");                if (linkPropertIEs == null) {                    throw new IllegalArgumentException(                            "required argument can not be blank.");                }                Class<?> laClass = Class.forname("androID.net.linkAddress");                Constructor<?> laConstructor = laClass                        .getConstructor(new Class[] { InetAddress.class,int.class });                Object linkAddress = laConstructor.newInstance(inetAddr,networkPrefixLength);                Class<?> setIpAddress = Class                        .forname("androID.net.linkPropertIEs");                Boolean result = (Boolean) invokeDeclaredMethod(setIpAddress,linkPropertIEs,"addlinkAddress",new Class[] { laClass },new Object[] { linkAddress });            } catch (Exception e) {                e.printstacktrace();            }        }        /**         * This API is used for setting gateway in wifiConfigration         *          * @param gateway         * @param wifiConf         */        private static voID setGateway(String gateway,WifiConfiguration wifiConf) throws SecurityException,InvocationTargetException {            try {                if (TextUtils.isEmpty(gateway)) {                    throw new IllegalArgumentException(                            "required argument can not be blank.");                }                InetAddress inetAddr = null;                Class networkUtils = Class.forname("androID.net.NetworkUtils");                Method numericToInetAddress = networkUtils.getDeclaredMethod(                        "numericToInetAddress",gateway.getClass());                inetAddr = (InetAddress) numericToInetAddress.invoke(null,gateway);                Object linkPropertIEs = getFIEldValue(wifiConf.getClass(),"linkPropertIEs");                if (linkPropertIEs == null) {                    throw new IllegalArgumentException(                            "required argument can not be blank.");                }                Class routeInfoClass = Class.forname("androID.net.RouteInfo");                Constructor routeInfoConstructor = routeInfoClass                        .getConstructor(new Class[] { InetAddress.class });                Object routeInfo = routeInfoConstructor.newInstance(inetAddr);                Class<?> linkPropertIEsClass = Class                        .forname("androID.net.linkPropertIEs");                Boolean result = (Boolean) invokeDeclaredMethod(                        linkPropertIEsClass,"addRoute",new Class[] { routeInfoClass },new Object[] { routeInfo });            } catch (Exception e) {                e.printstacktrace();            }        }        /**         * This API is used for setting DNS in wifiConfigration         *          * @param dns         * @param wifiConf         * @throws NoSuchMethodException         * @throws InvocationTargetException         */        private static voID setDNS(WifiSetting wifiSettings,InvocationTargetException {            try {                String dns1 = wifiSettings.getDns1();                String dns2 = wifiSettings.getDns2();                if (TextUtils.isEmpty(dns1) && TextUtils.isEmpty(dns2)) {                    throw new IllegalArgumentException(                            "required argument can not be blank.");                }                Class dnsInfo = Class.forname("androID.net.NetworkUtils");                Method sethttpProxy = dnsInfo.getDeclaredMethod(                        "numericToInetAddress",String.class);                InetAddress inetAddressDns1 = (InetAddress) sethttpProxy                        .invoke(null,dns1);                InetAddress inetAddressDns2 = (InetAddress) sethttpProxy                        .invoke(null,dns2);                Object linkPropertIEs = getFIEldValue(wifiConf.getClass(),"linkPropertIEs");                if (linkPropertIEs == null) {                    throw new IllegalArgumentException(                            "required argument can not be blank.");                }                Class<?> linkPropertIEsClass = Class                        .forname("androID.net.linkPropertIEs");                Class<?> inetAddressClass = Class                        .forname("java.net.InetAddress");                invokeDeclaredMethod(linkPropertIEsClass,"addDns",new Class[] { inetAddressClass },new Object[] { inetAddressDns1 });                invokeDeclaredMethod(linkPropertIEsClass,new Object[] { inetAddressDns2 });            } catch (ClassNotFoundException e) {                // Todo auto-generated catch block                e.printstacktrace();            } catch (remoteexception e) {                // Todo auto-generated catch block                e.printstacktrace();            }        }        /**         * This API is used for setting Proxy in wifiConfigration         *          * @param proxyHostname         * @param proxyPort         * @param proxyExceptions         * @param config         */        private static voID setWifiProxySettings(WifiSetting wifiSettings,WifiConfiguration config) {            try {                if (null == config) {                    throw new IllegalArgumentException(                            "required argument can not be blank.");                }                // get the link propertIEs from the wifi configuration                Object linkPropertIEs = getFIEldValue(config.getClass(),config,"linkPropertIEs");                if (null == linkPropertIEs) {                    throw new IllegalArgumentException(                            "required argument can not be blank.");                }                // get the sethttpProxy method for linkPropertIEs                Class proxyPropertIEsClass = Class                        .forname("androID.net.ProxyPropertIEs");                Class[] sethttpProxyParams = new Class[1];                sethttpProxyParams[0] = proxyPropertIEsClass;                Class lpClass = Class.forname("androID.net.linkPropertIEs");                Method sethttpProxy = lpClass.getDeclaredMethod("sethttpProxy",sethttpProxyParams);                sethttpProxy.setAccessible(true);                // get ProxyPropertIEs constructor                Class[] proxyPropertIEsCtorParamTypes = new Class[3];                proxyPropertIEsCtorParamTypes[0] = String.class;                proxyPropertIEsCtorParamTypes[1] = int.class;                proxyPropertIEsCtorParamTypes[2] = String.class;                Constructor proxyPropertIEsCtor = proxyPropertIEsClass                        .getConstructor(proxyPropertIEsCtorParamTypes);                // create the parameters for the constructor                Object[] proxyPropertIEsCtorParams = new Object[3];                proxyPropertIEsCtorParams[0] = wifiSettings.getProxyHostname();                proxyPropertIEsCtorParams[1] = wifiSettings.getProxyPort();                proxyPropertIEsCtorParams[2] = wifiSettings                        .getProxyExceptions();                // create a new object using the params                Object proxySettings = proxyPropertIEsCtor                        .newInstance(proxyPropertIEsCtorParams);                // pass the new object to sethttpProxy                Object[] params = new Object[1];                params[0] = proxySettings;                sethttpProxy.invoke(linkPropertIEs,params);                setEnumFIEld(config,"proxySettings");            } catch (Exception e) {                e.printstacktrace();            }        }    public static Object invokeDeclaredMethod(Class<?> clazz,Object object,String methodname,Class<?>[] args,Object[] argsValue)            throws SecurityException,InvocationTargetException,remoteexception {        LogUtil.d(TAG,"Invoking declared method " + clazz.getSimplename()                + "." + methodname);        Method privateMethod = clazz.getDeclaredMethod(methodname,args);        privateMethod.setAccessible(true);        Object result = privateMethod.invoke(object,argsValue);        return result;    }    private static voID setEnumFIEld(Object object,String value,String name)            throws SecurityException,illegalaccessexception {        FIEld f = object.getClass().getFIEld(name);        f.set(object,Enum.valueOf((Class<Enum>) f.getType(),value));    }    public static Object getFIEldValue(Class<?> clazz,String fIEldname) {        if (clazz == null || fIEldname == null) {            throw new IllegalArgumentException(                    "required argument can not be blank.");        }        Object result = null;        try {            FIEld f = clazz.getFIEld(fIEldname);            f.setAccessible(true);            result = f.get(object);        } catch (Exception e) {            e.printstacktrace();            throw new RuntimeException(e.getMessage());        }        return result;    }

编辑:API for LolliPOP [上面的API for proxy和ip不会在最新的Andriod L中工作]

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LolliPOP) {            // Implementation for Proxy setting for LolliPOP            try {                URL proxyUrl = new URL(                        addNetworkProxyPropertIEs.gethttpProxyUri());                String host = proxyUrl.getHost();                int portStr = proxyUrl.getPort();                String exclusionList = addNetworkProxyPropertIEs                        .getExceptions();                Constructor<ProxyInfo> constructor = ProxyInfo.class                        .getConstructor(new Class[] { String.class,int.class,String.class });                ProxyInfo mhttpProxy = constructor.newInstance(new Object[] {                        host,portStr,exclusionList });                Class ipConfigClass = Class                        .forname("androID.net.IpConfiguration");                Object ipConfigObject = ipConfigClass.newInstance();                Method sethttpProxy = ipConfigClass.getDeclaredMethod(                        "sethttpProxy",ProxyInfo.class);                sethttpProxy.setAccessible(true);                sethttpProxy.invoke(ipConfigObject,mhttpProxy);                Method gethttpProxySettings = ipConfigClass                        .getDeclaredMethod("getProxySettings");                gethttpProxySettings.setAccessible(true);                Method setProxy = config.getClass().getDeclaredMethod(                        "setProxy",gethttpProxySettings.invoke(ipConfigObject).getClass(),ProxyInfo.class);                setProxy.setAccessible(true);                setEnumFIEld(ipConfigObject,"proxySettings");                setProxy.invoke(config,gethttpProxySettings.invoke(ipConfigObject),mhttpProxy);            } catch (Exception e) {                /*                 * Excepted exceptions may be SecurityException,* IllegalArgumentException,* InvocationTargetException,* InstantiationException,* NoSuchFIEldException,MalformedURLException                 */                e.printstacktrace();            }        }
总结

以上是内存溢出为你收集整理的Android中的手动代理通过反射全部内容,希望文章能够帮你解决Android中的手动代理通过反射所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1127648.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存