【问题标题】:Android HttpURLConnection with fiddler: IOException: socket is closed带有提琴手的Android HttpURLConnection:IOException:套接字已关闭
【发布时间】:2013-04-08 17:38:14
【问题描述】:

我为 Android 模拟器设置了 Fiddler2,它可以工作,我可以通过 Android 浏览器查看 HTTPS 流量,但我无法使用 HttpsURLConnection 使用我的代码连接到 HTTPS 服务器:

    // use Fiddler's proxy:
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("172.16.4.20", 8888));
    HttpsURLConnection connection = (HttpsURLConnection) new URL(url)
            .openConnection(proxy);

    connection.setRequestMethod(method);
    connection.setRequestProperty("Content-Type", contentType);
    connection.setDoInput(true);
    connection.setDoOutput(true);
    OutputStream out = connection.getOutputStream();

我明白了:

java.net.SocketException: Socket is closed

在 Fiddler 中我可以看到第一个请求(我删除了主机地址)

CONNECT [address]:443 HTTP/1.1
Host: [address] 
User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.0.4; Android SDK built for x86 Build/IMM76D)
Connection: Keep-Alive

After the client received notice of the established CONNECT, it failed to send any data.

回复:

HTTP/1.1 200 Connection Established
FiddlerGateway: Direct
StartTime: 12:32:18.567
Connection: close

上述请求响应对与任何其他 Android 浏览器 HTTPS 连接建立类似。

之后我无法发送我的 POST 请求(因为我收到了 SocketException)。没有代理也可以。

【问题讨论】:

    标签: android https proxy httpurlconnection fiddler


    【解决方案1】:

    最可能的解释是客户端关闭连接是因为它对 Fiddler 的 HTTPS 解密证书不满意。您是否已将设备配置为信任 FiddlerRoot.cer 证书文件?您是否已将事件处理程序附加到您的 HttpsURLConnection 以接受 Fiddler 的自签名证书?

    【讨论】:

    • 是的,我添加了 FiddlerRoot.cer 并且我信任所有证书。就像我在 Android 浏览器中所说的,HTTPS 网站一切正常,我可以在 Fiddler 中看到流量。
    • 这表明应用程序没有从浏览器继承证书信任。您是否已将事件处理程序附加到您的 HttpsURLConnection 以接受 Fiddler 的自签名证书?
    • 我遇到了完全相同的问题。我已经在我的设备上安装了 FiddlerRoot.cer,就像 Attila 说的我可以从 Android 浏览器和 Chrome 中嗅探 https 流量。您是什么意思“将事件处理程序附加到您的 HttpsURLConnection 以接受 Fillder 的自签名证书”?我已经实现了一个虚拟的 TrustManager,它什么都不做,并将它用作 SSLSocketFactory 的一部分。但它没有工作。我仍然收到 SocketException 说“套接字已关闭”
    • @TakashiOguma:您可能应该打开一个新问题,其中包括您正在使用的代码,以及有关版本的重现信息以及您的虚拟 TrustManager 是否被调用等。
    【解决方案2】:

    我在 iPhone 上遇到了同样的问题。
    我所做的是访问这个 url
    ht_tp(去掉下面的分数)://(replace_with_the_ip_of_the_host_where_fiddler_is_running):8888
    从您的移动设备(在您的情况下是您模拟的 Android 设备) 然后你会看到这个链接上描述的网页 click this
    您需要安装证书。之后,您将在 fiddler 中显示响应。

    享受吧!

    【讨论】:

    • 这在应用程序中不起作用,只会帮助捕获浏览器流量
    • 它至少在物理设备上为我工作,并正确设置了 Http 代理。
    【解决方案3】:

    原因可能是应用使用了证书固定。它不信任设备“信任库”中的任何证书,而是配置为仅信任特定证书(由服务器签名)。

    所以当 Fiddler 出示自己的证书时,客户端会报错。

    更多详情:https://security.stackexchange.com/questions/29988/what-is-certificate-pinning

    【讨论】:

      猜你喜欢
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 2015-02-17
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      相关资源
      最近更新 更多