将 CONNECT 与 HttpURLCOnnection 结合使用

Use CONNECT with HttpURLCOnnection

我正在尝试获取服务器在连接时发送的数据,

HttpURLConnection con = (HttpURLConnection) new URL("http://myurl.com:443").openConnection();

con.setRequestMethod("CONNECT");

BufferedReader reader;

if(con.getResponseCode() >= 100 && con.getResponseCode() < 400) {
    reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
}else {
    reader = new BufferedReader(new InputStreamReader(con.getErrorStream()));
}

String line;
while((line = reader.readLine()) != null) {
    System.out.println(line);
}

但我收到错误

Invalid HTTP method: CONNECT

为什么我不能使用 CONNECT 作为 RequestMethod?

来自文档 HttpURLConnection.setRequestMethod :

setRequestMethod

public void setRequestMethod(String method) throws ProtocolException

Set the method for the URL request, one of:
GET
POST
HEAD
OPTIONS
PUT
DELETE
TRACE

are legal, subject to protocol restrictions. The default method is GET.
Parameters:
method - the HTTP method
Throws:
ProtocolException - if the method cannot be reset or if the requested method isn't valid for HTTP.
SecurityException - if a security manager is set and the method is "TRACE", but the "allowHttpTrace" NetPermission is not granted.
See Also:
getRequestMethod()

根据 java 文档,您只能将 GET POST HEAD OPTIONS PUT DELETE TRACE 设置为请求方法。 Connect 不是有效的请求方法。