【问题标题】:How to prevent CONNECT method call from HttpsURLConnection如何防止来自 HttpsURLConnection 的 CONNECT 方法调用
【发布时间】:2014-01-23 05:42:25
【问题描述】:

我有一个 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

由于“代理连接”标头,我认为这是与代理相关的

【问题讨论】:

    标签: java android httpurlconnection


    【解决方案1】:

    URLConnection 的默认设置是池化 TCP(套接字)连接。似乎“Proxy-Connection”标头具有误导性,因为当“Connection”真正意图时,该标头可能被盗用。

    当我设置系统属性时

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

    为了禁用连接池,CONNECT 方法请求条目消失了。

    由于需要为每个请求创建 Socket,但应用程序不会发出大量请求,因此性能会受到轻微影响,因此这是可以接受的。

    【讨论】:

      【解决方案2】:

      只有在您配置了 HTTP 代理时才会发生这种情况。可能您设置了系统属性http.proxyHosthttp.proxyPort,,或者Android 有其他配置方式。

      【讨论】:

        猜你喜欢
        • 2016-01-27
        • 2022-11-26
        • 1970-01-01
        • 2014-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多