Android设置反代理
转自:https://blog.csdn.net/hizhangyuping/article/details/82143762?utm_source=blogxgwz1
虽然类似fiddler之类的抓包工具很好用,只要在客户端设置一下手动代理就可以轻松获取接口数据。但是为了数据安全性着想,需要在网络框架中设置一下反代理。具体如下:
httpBuilder = OkHttpClient.Builder()
.addInterceptor(defaultInterceptor())
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
.readTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
.proxy(Proxy.NO_PROXY)
//没有代理
判断是否有代理
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));
}