Android设置反代理

虽然类似fiddler之类的抓包工具很好用,只要在客户端设置一下手动代理就可以轻松获取接口数据。但是为了数据安全性着想,需要在网络框架中设置一下反代理。具体如下:

var httpBuilder = OkHttpClient.Builder()
            .addInterceptor(defaultInterceptor())
            .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
            .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
            .readTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
            .proxy(Proxy.NO_PROXY)//没有代理

判断是否有代理

    @Test
    public void checkWifiProxy() {
        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(!TextUtils.isEmpty(portStr) ? portStr : "-1");
        } else {
            proxyAddress = android.net.Proxy.getHost(appContext);
            proxyPort = android.net.Proxy.getPort(appContext);
        }
        Log.d("cxmyDev", "proxyAddress :" + proxyAddress + ", prot : " + proxyPort);
        Log.d("cxmyDev", "checkWifiProxy: "+(!TextUtils.isEmpty(proxyAddress) && proxyPort != -1));
    }

你可能感兴趣的:(Android,安全性)