通过 HTTP 代理的 HTTPS

HTTPS through HTTP proxy

我有代理服务,你可以通过它获取网页。例如通过 telnet

GET http://example.com HTTP/1.1
Host: example.com

但如果我想下载 https 页面,我应该执行以下操作

GET https://example.com HTTP/1.1
Host: example.com
Https-Header: true

我想使用 apache http 客户端为该服务编写 scala 客户端,使用类似代理主机的服务。

private val DefaultProxy = new HttpHost("service host", port)
private val DefaultClient =
HttpClientBuilder.create().
  setProxy(DefaultProxy).
  build()

我可以成功下载 http 页面,但是当我尝试下载 https 页面时,apache 客户端向代理发出 CONNECT 请求,它响应错误,导致服务只能使用 GET 请求运行。 我怎样才能使 apache 客户端像使用 http 一样使用 https 页面,这意味着向代理发送 GET 请求,而不是 CONNECT?

要以与使用 telnet 的 http 网页相同的方式下载 https 网页,您需要先建立 ssl/tls 连接:

openssl s_client -connect www.somesite:443

[watch the ssl certificate details scroll by]

GET /index.html HTTP/1.1

Host: www.somesite

示例来自 https://www.bearfruit.org/2008/04/17/telnet-for-testing-ssl-https-websites/

对于 scala 也许可以帮助你:https://github.com/scalaj/scalaj-http

HTTPS 是基于 SSL/TLS 的 HTTP,因此您需要一些东西来建立到网站的 SSL/TLS 安全隧道,然后您可以发送 HTTP 请求。

我找到了解决办法。

我编写了始终提供不安全路由的自定义 HttpRoutePlanner,然后 Apache 客户端使用 https link 就像使用 http link 一样,有一个 HttpRoutePlanner 代码

private def routePlanner(proxy: HttpHost) = new HttpRoutePlanner() {

  def determineRoute(target: HttpHost ,
                     request: HttpRequest,
                     context: HttpContext) = {
    new HttpRoute(target, null,  proxy, false)
  }
}