java - 如何从 GAE servlet 发送 HTTPS 请求?

标签 java google-app-engine https

我正在尝试使用 GAE 技术发送 https 请求。但如果我使用

HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

它会崩溃

java.lang.ClassCastException: com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection cannot be cast to javax.net.ssl.HttpsURLConnection

如果我将 HttpsURLConnection 更改为 HttpURLConnection,一切都会正常,但我认为 https 的优势不会被利用。那么如何才能正确发送https请求呢?

最佳答案

我假设,objjava.net.URL 类型。 简而言之,在这种情况下您不应该使用 javax.net.ssl.HttpsURLConnection。

URL#openConnection() 方法返回类型 java.net.URLConnection,并且不提供对其子类型的几乎任何保证(除了奇怪的 javadoc 描述之外,请参见下面*),Google 就利用了它。 您的应用程序不应依赖于 HttpsURLConnection 的细节。有两种选择:

  • 仅使用 URLConnection - 由 API 保证,
  • 使用 HttpURLConnection 作为 URLFetchServiceStreamHandler$Connection 扩展它,并且不太可能更改。

在这两种情况下,都将使用 HTTPS 功能;转换不会改变对象的性质。

其他情况是 GAE 中的客户端证书 - 请参阅 Client Authentication by Certificate in GAE java问题。

*奇怪的是,URL.openConnection 的 javadoc 说:

If for the URL's protocol (such as HTTP or JAR), there exists a public, specialized URLConnection subclass belonging to one of the following packages or one of their subpackages: java.lang, java.io, java.util, java.net, the connection returned will be of that subclass. For example, for HTTP an HttpURLConnection will be returned, and for JAR a JarURLConnection will be returned.

(来源:https://docs.oracle.com/javase/8/docs/api/java/net/URL.html#openConnection)

HttpsConnction 不在任何提到的包中,因此 Google 可以自由地提供它想要的任何类型。但这样的描述提供了将来将 HttpsURLConnection 添加到 - 例如 - java.util 包的可能性,然后 AppEngine 实现将不再相关。这只是猜测,而且很可能永远不会发生 - 但不能保证! :)

关于java - 如何从 GAE servlet 发送 HTTPS 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32046562/

相关文章:

html - CSS 文件仅部分适用于 Google App Engine

java - 运行本地 GAE 应用程序时出现问题 : Unresolved compilation problems

ruby-on-rails - 如何禁用 HTTP 严格传输安全?

java - 滚动 Pane 不显示内容

java - 单击按钮时没有响应

java - Powermock withArguments 调用变量输入

google-app-engine - 如果最近的事务引发 "special"异常,事务性 db.get() 是否可能返回过时的结果?

javascript - AWS lambda : Error: getaddrinfo ENOTFOUND from api

wcf - 使用 http 和 https 绑定(bind)/端点部署 WCF 服务

java - Selenium chromedriver 2.27.440174 无法运行 Google Chrome 55.0.2883.87 m