现象:charles抓不到包,但wireshark,HttpAnalyzor可以抓到包。
关键代码:
[java] view plain copy URL url = new URL(urlStr); urlConnection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
[java] view plain copy OkHttpClient client = new OkHttpClient().newBuilder().proxy(Proxy.NO_PROXY).build();
Android 如果防止APK被抓包工具抓包
转:http://blog.csdn.net/a6136581/article/details/72627026
平时都是用Fiddler对Android应用进行抓包,如果对Fiddler不熟悉,可以点击查看Fiddler抓包方法。在抓抱前,需要将手机的WiFi进行代理设置,然后才能在电脑上使用Fiddler成功抓包,那么我们试想一下,如果在APP请求网络之前先判断下手机网络是否使用了代理,如果使用了代理就不请求接口。新的问题来了,在Android手机中如何知道手机网络是否使用了代理?
经过查找资料与实践,发现如下的代码能够满足需求:
[java] view plain copy private boolean isWifiProxy() { 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(this); proxyPort = android.net.Proxy.getPort(this); } return (!TextUtils.isEmpty(proxyAddress)) && (proxyPort != -1); }
JAVA基础知识之网络编程——-使用Proxy创建连接
转:https://www.cnblogs.com/fysola/p/6089416.html
在前面的HTTP网络通信的例子中,使用了URLConnection conn = url.openConnection();连接网络,
如果改用URLConnection conn = url.openConnection(proxy);方式,传入一个proxy对象,设置好代理IP和端口,则可以实现代理连接,
下面是一个简单例子,
[java] view plain copy package proxy; import java.io.IOException; import java.io.PrintStream; import java.net.InetSocketAddress; import java.net.MalformedURLException; import java.net.Proxy; import java.net.URL; import java.net.URLConnection; import java.util.Scanner; public class ProxyTest { final String PROXY_ADDR = "172.20.230.5"; final int PROXY_PORT = 3128; String urlStr = "http://www.baidu.com"; //String urlStr = "http://www.crazyit.org"; public void init() throws IOException { URL url = new URL(urlStr); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_ADDR, PROXY_PORT)); //使用代理服务器打开链接 URLConnection conn = url.openConnection(proxy); //URLConnection conn = url.openConnection(); conn.setConnectTimeout(5000); try { Scanner scan = new Scanner(conn.getInputStream()); PrintStream ps = new PrintStream("index.html"); while (scan.hasNextLine()) { String line = scan.nextLine(); System.out.println(line); ps.println(line); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { new ProxyTest().init(); } }
在上面的例子中,每次用url对象open一个connection的时候,都需要显示地传入一个proxy对象才行。
而实际上可以在connection之前,做一个默认代理设置,这样以后再openConnection的时候,就不需要显示传入proxy对象了。
做默认代理设置需要重写ProxySelector的select方法,返回代理IP和端口列表,具体实现如下,
[java] view plain copy package proxy; import java.io.IOException; import java.io.PrintStream; import java.net.InetSocketAddress; import java.net.MalformedURLException; import java.net.Proxy; import java.net.ProxySelector; import java.net.SocketAddress; import java.net.URI; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class ProxyTest { final String PROXY_ADDR = "172.20.230.5"; final int PROXY_PORT = 3128; String urlStr = "http://www.baidu.com"; //String urlStr = "http://www.crazyit.org"; public void init() throws IOException { ProxySelector.setDefault(new ProxySelector(){ @Override public void connectFailed(URI arg0, SocketAddress arg1, IOException arg2) { System.out.println("无法连接到服务器"); } @Override public List<Proxy> select(URI uri) { List<Proxy> result = new ArrayList<>(); result.add(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_ADDR, PROXY_PORT))); return result; } }); URL url = new URL(urlStr); //使用代理服务器打开链接 URLConnection conn = url.openConnection(); conn.setConnectTimeout(5000); try { Scanner scan = new Scanner(conn.getInputStream()); PrintStream ps = new PrintStream("index.html"); while (scan.hasNextLine()) { String line = scan.nextLine(); System.out.println(line); ps.println(line); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { new ProxyTest().init(); } }可以看到使用代理之后,再用url打开链接时就能像普通连接那样url.openConnection();了
Android 开发之避免被第三方使用代理抓包
转:http://blog.csdn.net/a807891033/article/details/53643550
最近遇到一个问题,手机wifi设置了一个可用代理后,然后再到自己的应用中耍,发现了一个大悲剧,
就是所有接口都访问不了,全部进了异常中,瞬间就两眼泪汪汪了,后来问了度娘,给出的一个还能用的
解释:
[java] view plain copy System.getProperties().remove("http.proxyHost"); System.getProperties().remove("http.proxyPort"); System.getProperties().remove("https.proxyHost"); System.getProperties().remove("https.proxyPort"); 每错,这就是移除所有代理,但问题是如果移除全部,那蜗牛其他的功能就必会受一些影响,没办法,只能
自己去看SDK,果然,Android是提供单个接口访问不带代理的,不废话,上代码
[java] view plain copy URL url = new URL(urlStr); urlConnection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
嗯,很实在,然后蜗牛又去看了一下OKhttp框架是否提供类似的操作,·······果然不愧是好框架:
[java] view plain copy OkHttpClient client = new OkHttpClient().newBuilder().proxy(Proxy.NO_PROXY).build();
好了,蜗牛就只弄了这点,有错请指出!!!!
自己调试的界面:
转载于:https://www.cnblogs.com/c-x-a/p/9174663.html