How to prevent CONNECT method call from HttpsURLConnection

故事扮演 提交于 2019-12-08 01:54:33

问题


I have an Android client making HTTPS request to a server. The firewall logs have entries for CONNECT request methods which are not desired.

When would the CONNECT request get sent out and how can I prevent it from being sent? I expect only a GET request. My understanding is that the call to openConnection() does not actually make a request and that the GET request would go on the call to getResponseMessage().

How can I disable the http client from attempting to establish a proxy tunnel?

Here is how I sent up my connection and make the call:

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();

EDIT:

Here is the Firewall Log entry that I am trying to prevent:

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

I thought this was proxy related because of the "Proxy-Connection" header


回答1:


The default for URLConnection is to pool the TCP (socket) connections. It seems that the "Proxy-Connection" header was misleading as that header can be misappropriated when "Connection" is really intended.

When I set the system property

System.setProperty("http.keepAlive", "false");

to disable the connection pool, the CONNECT method request entries went away.

There is a slight performance hit since Sockets need to be created for each request but the app does not make a significant number of requests so this is acceptable.




回答2:


This only happens when you have an HTTP proxy configured. Possibly you have set the system properties http.proxyHost and http.proxyPort, or maybe Android has another way of configuring this.



来源:https://stackoverflow.com/questions/21150568/how-to-prevent-connect-method-call-from-httpsurlconnection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!