我的代码支持http,但不支持https。它显示了java.net.SocketException:来自服务器的意外文件结尾
我试图向服务器发送一个我的代码支持http,但不支持https。它显示了java.net.SocketException:来自服务器的意外文件结尾,java,http,ssl,https,Java,Http,Ssl,Https,我试图向服务器发送一个GET请求(包含有关设备的详细信息)并从中获取响应。响应中应包含有关设备的详细信息。这些详细信息是设备Id和设备类型。 下面是我的代码 我用xxx.xx替换了Id地址。。。对不起 我在httpport(8482)中尝试了一次。 这是一个很好的解决方案。但实际上我想把这个程序用于https端口(8120) 那么我该怎么做才能让这段代码获得https的支持呢 void callConfigEndPoint() { String host = "xxx.xxx.xxx.x
GET
请求(包含有关设备的详细信息)并从中获取响应。响应中应包含有关设备的详细信息。这些详细信息是设备Id
和设备类型
。
下面是我的代码
我用xxx.xx替换了Id地址。。。对不起
我在http
port(8482)中尝试了一次。
这是一个很好的解决方案。但实际上我想把这个程序用于https
端口(8120)
那么我该怎么做才能让这段代码获得https的支持呢
void callConfigEndPoint() {
String host = "xxx.xxx.xxx.xxx";
String httpPort = "8482";
String deviceToken = "7645221";
String USER_AGENT = "Mozilla/5.0";
String endpointUrl = "http://"+host+":"+httpPort+"/api/device-mgt-config/v1.0";
try {
URL urlObject = new URL(endpointUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) urlObject.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("token", deviceToken);
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONObject jsonObj = new JSONObject(response.toString());
String deviceId = jsonObj.get("deviceId").toString();
String deviceType = jsonObj.get("deviceType").toString();
System.out.println("deviceId =>" + deviceId);
System.out.println("deviceType =>" + deviceType);
} catch (IOException e) {
e.printStackTrace();
}
}
请考虑一下,我已经把这个导入部分从这个
中删除了,我看到你使用了<代码> HTTURL连接< /代码>,你试过用<代码> HTTPSURLCONNECT/<代码>吗?不,你说它会成功吗?您可以将此代码更改为https吗?从未亲自尝试过,但由于HttpsURLConnection
是HttpURLConnection
的子类,因此我认为无需在其他地方编辑代码,它就可以工作。你试过了吗?