使用 java 类 HttpsURLConnection

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3038026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:53:26  来源:igfitidea点击:

Using java class HttpsURLConnection

javahttps

提问by KB22

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:

我有一小段代码,它基本上实现了一个 HTTP 客户端,即它 POSTS 请求并与 re RESPONSE 一起工作。只要关注 HTTP,一切都运行良好。出于某种原因,我现在也必须支持 HTTPS。所以这里是我为了打开连接所做的简要说明:

 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!

任何想法表示赞赏!

回答by BalusC

Just keep it java.net.URLConnectionor cast it to java.net.HttpURLConnectioninstead. Both offers methods to do the desired task as good.

只需保留它java.net.URLConnection或将其转换为java.net.HttpURLConnection。两者都提供了很好地完成所需任务的方法。



A side remark unrelated to the technical problem: you should neverexplicitly 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.

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



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

更新:由于您似乎不小心导入了它,请转到Window > Preferences > Java > Appearance > Type FiltersAddcom.sun.*添加sun.*到列表中。这样你就不会意外导入它们:

enter image description here

在此处输入图片说明

回答by Mike Baranczak

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.

如果没有看到整个文件就很难说,但是当您真的需要 javax.net.ssl.HttpsURLConnection 时,您似乎正在导入 com.sun.net.ssl.HttpsURLConnection。

回答by DaveJohnston

Check your imports, you should be using

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

java.net.HttpURLConnection

or

或者

javax.net.ssl.HttpsURLConnection

回答by jsaevsah

Instead of creating a URL object using standard constructor like

而不是使用标准构造函数创建 URL 对象,如

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

这将解决这个问题

回答by Dayanand Fagare

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

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

回答by Salman Zafar

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

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

回答by user2505915

The above problem is only caused by two issues

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

  1. Using wrong import
  2. Using httpin the string you create url from use instead https
  1. 使用错误的导入
  2. 使用http字符串中你使用创建网址,而不是https

回答by Geanluca

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

回答by AJC

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

就我而言,调用 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.

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