我有一个 Android 客户端向服务器发出 HTTPS 请求。防火墙日志包含不需要的 CONNECT 请求方法条目。
CONNECT 请求何时发出以及如何阻止它发送?我只期望一个 GET 请求。我的理解是,对 openConnection() 的调用实际上并不发出请求,并且 GET 请求将继续对 getResponseMessage() 的调用。
如何禁止 http 客户端尝试建立代理隧道?
以下是我发送连接并调用电话的方式:
URL url = new URL("https://some.server.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setConnectTimeout(CONNECT_TIMEOUT);
connection.setReadTimeout(REAT_TIMEOUT);
connection.setRequestProperty(ACCEPT_CHARSET, UTF8_CHARSET);
connection.setRequestProperty(CONTENT_TYPE_HEADER, contentType);
setCustomRequestProperties(connection);
connection.setRequestMethod("GET");
//create response
connection.getResponseMessage();
connection.getResponseCode();
connection.getContentType();
connection.getContent();
编辑:
这是我试图阻止的防火墙日志条目:
CONNECT someURL.domain.com:443 HTTP/1.1
Host: someURL.domain.com
User-Agent: CustomUserAgent;1.0.0(Android 4.3;Nexus 4;T-Mobile)
Proxy-Connection: Keep-Alive
X-SSL-Secure: true
X-CS-Source-IP: 10.3.3.3
X-Forwarded-For: 10.3.3.3
由于“Proxy-Connection” header ,我认为这与代理相关
最佳答案
URLConnection 的默认设置是池化 TCP(套接字)连接。看来“Proxy-Connection” header 具有误导性,因为当真正打算使用“Connection”时,该 header 可能会被盗用。
当我设置系统属性时
System.setProperty("http.keepAlive", "false");
要禁用连接池,CONNECT 方法请求条目就会消失。
由于需要为每个请求创建套接字,因此性能会受到轻微影响,但应用程序不会发出大量请求,因此这是可以接受的。
关于java - 如何防止从 HttpsURLConnection 调用 CONNECT 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21150568/