Android 禁止代理抓包
阿新 • • 發佈:2018-11-05
原文地址:https://blog.csdn.net/b799841701/article/details/78611766
以前一直使用fiddler進行抓包測試,有次看到一個apk,想抓包看下資料,發現使用fiddler是無法抓取到資源請求外的http請求,並且確定他是http請求,但就是抓取不到,後來查了下相關資料,看到我們平時使用的http請求時:
URL url1 = new URL("url");
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
基本是這麼一種寫法,開啟openConnection發現,裡面有兩種呼叫
public URLConnection openConnection() throws java.io.IOException { return handler.openConnection(this); } /** * Same as {@link #openConnection()}, except that the connection will be * made through the specified proxy; Protocol handlers that do not * support proxing will ignore the proxy parameter and make a * normal connection. * * Invoking this method preempts the system's default ProxySelector * settings. * * @param proxy the Proxy through which this connection * will be made. If direct connection is desired, * Proxy.NO_PROXY should be specified. * @return a <code>URLConnection</code> to the URL. * @exception IOException if an I/O exception occurs. * @exception SecurityException if a security manager is present * and the caller doesn't have permission to connect * to the proxy. * @exception IllegalArgumentException will be thrown if proxy is null, * or proxy has the wrong type * @exception UnsupportedOperationException if the subclass that * implements the protocol handler doesn't support * this method. * @see java.net.URL#URL(java.lang.String, java.lang.String, * int, java.lang.String) * @see java.net.URLConnection * @see java.net.URLStreamHandler#openConnection(java.net.URL, * java.net.Proxy) * @since 1.5 */ public URLConnection openConnection(Proxy proxy) throws java.io.IOException { if (proxy == null) { throw new IllegalArgumentException("proxy can not be null"); } // Create a copy of Proxy as a security measure Proxy p = proxy == Proxy.NO_PROXY ? Proxy.NO_PROXY : sun.net.ApplicationProxy.create(proxy); SecurityManager sm = System.getSecurityManager(); if (p.type() != Proxy.Type.DIRECT && sm != null) { InetSocketAddress epoint = (InetSocketAddress) p.address(); if (epoint.isUnresolved()) sm.checkConnect(epoint.getHostName(), epoint.getPort()); else sm.checkConnect(epoint.getAddress().getHostAddress(), epoint.getPort()); } return handler.openConnection(this, p); }
可以看到第二種裡面有一個proxy代理設定,並且代理引數的說明是
proxy the Proxy through which this connection will be made. If direct connection is desired,
Proxy.NO_PROXY should be specified.
意思就是說 如果需要直接連線(直連?大概就是繞過代理吧。。。我也不清楚具體實現),可以將代理設定為Proxy.NO_PROXY
這樣的話我們就可以這樣寫
URL url1 = new URL("url"); HttpURLConnection uc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
現在比較流行的網路請求框架一般是rxjava+retrofit+okhttp
同樣可以在OkHttpClient.Builder裡面進行設定
這樣就可以基本避免被fiddler之類的抓包工具抓包了