使用java类HttpsURLConnection
我有一小段代码,基本上实现了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
只需保留它
java.net.URLConnection< /code>
或将其转换为 <改为代码>java.net.HttpURLConnection
。两者都提供了完成所需任务的方法。与技术问题无关的旁注:您永远不应该在代码中显式导入/使用 Sun Java SE 实现特定的类。这些是未记录的类,可能会发生更改,在升级 JVM 时可能会导致代码中断。另一方面,当您在不同品牌的 JVM 上运行代码时,您的代码也可能会崩溃。
更新:由于您似乎不小心导入了它,请转到窗口> >首选项>爪哇>外观>输入过滤器并将添加
com.sun.*
和sun.*
到列表中。这样您就不会意外导入它们:Just keep it
java.net.URLConnection
or cast it tojava.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.*
andsun.*
to the list. This way you won't ever import them accidentally:您的 url 协议也应该是 https 而不是 http。检查你的网址。
Your url's protocol should also be https and not http. Check your url.
上述问题仅由两个问题引起
http
代替https
The above problem is only caused by two issues
http
in the string you create url from use insteadhttps
而不是使用像
Use
这样的标准构造函数创建 URL 对象来解决这个问题
Instead of creating a URL object using standard constructor like
Use
which would solve this problem
检查您的进口,您应该使用
或
Check your imports, you should be using
or
检查“serverAddress”变量的值。它应该是 https 而不是 http
Check value of your "serverAddress" variable. It should https and not http
如果没有看到整个文件,很难判断,但看起来您正在导入 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.
更改:
和
更改为:
和
Change:
and
Change To:
and
就我而言,调用 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.