我为 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)。没有代理它可以工作。