使用java类HttpsURLConnection [英] Using java class HttpsURLConnection

查看:206
本文介绍了使用java类HttpsURLConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一小段代码基本上要求HTTP-Client,
,即它的POSTS请求,并与re RESPONSE一起工作。只要HTTP是
,就可以很好地理解。出于某种原因,我现在也必须支持
HTTPS。所以这里简要介绍一下如何打开连接:

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();

此操作失败,说明:

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

我想这有点琐碎,但我只是不知道我在这个问题上做错了...
用Google搜索,代码看起来不对 - 不是吗?

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?

任何想法都表示赞赏!

推荐答案

保持不变它 java.net.URLConnection 或将其投射到 java.net.HttpURLConnection 。两者都提供了完成所需任务的方法。

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

与技术问题无关的侧面评论:你应该 never 在代码中显式导入/使用Sun Java SE实现特定的类。这些是未记录的类,并且可能会在升级JVM时导致代码中断。另一方面,当您在不同品牌的JVM上运行代码时,您的代码也可能会中断。

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.

更新:由于您似乎意外地导入了它,请转到窗口>首选项> Java>外观>类型过滤器添加 com。 sun。* sun。* 到列表中。这样你就不会意外地导入它们。 查看屏幕截图

Update: since you seem to accidently 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 accidently. See screenshot.

这篇关于使用java类HttpsURLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆