Android 禁止代理抓包

以前一直使用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 URLConnection 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里面进行设置

Android 禁止代理抓包_第1张图片

这样就可以基本避免被fiddler之类的抓包工具抓包了

你可能感兴趣的:(Android 禁止代理抓包)