HttpURLConnection 连接方法连接失败

HttpURLConnection connect method fails to connect

SocketAddress proxy = new InetSocketAddress("127.0.0.1", 8080);
URL url = new URL("http://192.168.1.1/");

HttpURLConnection connection = (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, proxy));
connection.setDoOutput(true);
String body = "This is a body example";

OutputStreamWriter writer = new OutputStreamWriter(new BufferedOutputStream(connection.getOutputStream()), "8859_1");
writer.write(body);
writer.flush();
writer.close();

connection.connect();

问题是,当我 运行 这段代码时,我的代理 "catched" 没有请求(配置良好)。我知道 connect() 是 URLConnection 中的一个抽象方法,但鉴于 HttpURLConnection 正在扩展 URLConnection 它应该覆盖它。这就是 javadoc 关于 connect() 的说法:"Opens a communications link to the resource referenced by this URL, if such a connection has not already been established." 所以应该已经发送了请求。任何人都知道导致问题的原因吗?

注意:如果我将 connection.connect() 替换为 connection.getResponseHeader() 我会收到请求。正如我在 javadoc 中读到的那样,如果尚未设置连接,则对 getResponseHeader() 的调用将隐式调用 connect() 方法。

This is what javadoc say about connect() : "Opens a communications link to the resource referenced by this URL, if such a connection has not already been established."

正确。

So the request should have been sent.

不合理。 你的报价中没有关于发送请求的内容。

如您所见,在您执行某些输入步骤之前,请求会被缓冲。