使用java类HttpsURLConnection

发布于 2024-09-05 05:08:58 字数 481 浏览 2 评论 0原文

我有一小段代码,基本上实现了 HTTP 客户端, 即它发布请求并与响应一起工作。只要 HTTP 是 关心的一切都运转良好。出于某种原因我现在必须支持 HTTPS 也是如此。因此,这里简要介绍一下我为了打开连接所做的事情:

 URL url = new URL(serverAddress);
 HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();

这失败了,说明:

sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to com.sun.net.ssl.HttpsURLConnection

我想这有点微不足道,但我只是不明白我在这方面做错了什么...... 谷歌搜索了一下,代码看起来不错——不是吗?

任何想法表示赞赏!

I have a small piece of code which basically impements a HTTP-Client,
i.e. it POSTS request and works with re RESPONSE. As long as HTTP is
concenerned everthing work well. For some reason I now have to support
HTTPS too. So here is briefly what I do in order to get a connection opened:

 URL url = new URL(serverAddress);
 HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();

This fails, stating:

sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to com.sun.net.ssl.HttpsURLConnection

I guess this is kinda trivial, but I just don't get what I'm doing wrong in this one...
Googled it, and the code just looks right - not?

any ideas are appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

凡间太子 2024-09-12 05:08:58

只需保留它 java.net.URLConnection< /code>或将其转换为 <改为代码>java.net.HttpURLConnection。两者都提供了完成所需任务的方法。


与技术问题无关的旁注:您永远不应该在代码中显式导入/使用 Sun Java SE 实现特定的类。这些是未记录的类,可能会发生更改,在升级 JVM 时可能会导致代码中断。另一方面,当您在不同品牌的 JVM 上运行代码时,您的代码也可能会崩溃。


更新:由于您似乎不小心导入了它,请转到窗口> >首选项>爪哇>外观>输入过滤器并将添加 com.sun.*sun.* 到列表中。这样您就不会意外导入它们:

在此处输入图像描述

Just keep it java.net.URLConnection or cast it to java.net.HttpURLConnection instead. Both offers methods to do the desired task as good.


A side remark unrelated to the technical problem: you should never explicitly import/use Sun Java SE implementation specific classes in your code. Those are undocumented classes and are subject to changes which may cause your code break when you upgrade the JVM. On the other hand, your code may also break when you run it at a different brand JVM.


Update: since you seem to accidentally have imported it, go to Window > Preferences > Java > Appearance > Type Filters and Add com.sun.* and sun.* to the list. This way you won't ever import them accidentally:

enter image description here

无畏 2024-09-12 05:08:58

您的 url 协议也应该是 https 而不是 http。检查你的网址。

Your url's protocol should also be https and not http. Check your url.

じ违心 2024-09-12 05:08:58

上述问题仅由两个问题引起

  1. 使用错误导入
  2. 在创建 url 的字符串中使用 http 代替 https

The above problem is only caused by two issues

  1. Using wrong import
  2. Using http in the string you create url from use instead https
彩虹直至黑白 2024-09-12 05:08:58

而不是使用像

URL wsURL = new URL(url);

Use

java.net.URL wsURL = new URL(null, url,new sun.net.www.protocol.https.Handler());

这样的标准构造函数创建 URL 对象来解决这个问题

Instead of creating a URL object using standard constructor like

URL wsURL = new URL(url);

Use

java.net.URL wsURL = new URL(null, url,new sun.net.www.protocol.https.Handler());

which would solve this problem

宫墨修音 2024-09-12 05:08:58

检查您的进口,您应该使用

java.net.HttpURLConnection

javax.net.ssl.HttpsURLConnection

Check your imports, you should be using

java.net.HttpURLConnection

or

javax.net.ssl.HttpsURLConnection
枯叶蝶 2024-09-12 05:08:58

检查“serverAddress”变量的值。它应该是 https 而不是 http

Check value of your "serverAddress" variable. It should https and not http

作业与我同在 2024-09-12 05:08:58

如果没有看到整个文件,很难判断,但看起来您正在导入 com.sun.net.ssl.HttpsURLConnection,而您确实需要 javax.net.ssl.HttpsURLConnection。

Hard to tell without seeing the whole file, but it looks like you're importing com.sun.net.ssl.HttpsURLConnection when you really want javax.net.ssl.HttpsURLConnection.

巨坚强 2024-09-12 05:08:58

更改:

import javax.net.ssl.HttpsURLConnection;

URL url = new URL(serverAddress);
HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();

更改为:

import java.net.HttpURLConnection;

URL url = new URL(serverAddress);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

Change:

import javax.net.ssl.HttpsURLConnection;

and

URL url = new URL(serverAddress);
HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();

Change To:

import java.net.HttpURLConnection;

and

URL url = new URL(serverAddress);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
腹黑女流氓 2024-09-12 05:08:58

就我而言,调用 httpsUrlConnection 时协议和端口不正确。

端口和协议被定义为静态类变量。失败步骤之前的步骤是调用 httpUrlConnection。该方法将端口/协议更改为 80/http,但最终没有将其设置回 /https。因此,尽管调用了 httpsUrlConnection,但它仍然使用 http/80。一旦我在 httpUrlConnection 方法末尾重置这些内容,错误就消失了。

In my case, the protocol and port were not correct while invoking the httpsUrlConnection.

Port and protocol were defined as static class variables. And the step prior to the failed step, was invoking an httpUrlConnection. That method changed the port/protocol to 80/http, but didn't set it back to /https at the end. So eventhough httpsUrlConnection was invoked, it was still using http/80. Once I reset those at the end of the httpUrlConnection method, the error disappeared.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文