33

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!

Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
KB22
  • 6,899
  • 9
  • 43
  • 52

9 Answers9

29

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

baudsp
  • 4,076
  • 1
  • 17
  • 35
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
19

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

Salman Zafar
  • 191
  • 1
  • 2
13

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
Amin Abu-Taleb
  • 4,423
  • 6
  • 33
  • 50
user2505915
  • 81
  • 1
  • 5
7

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

RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262
jsaevsah
  • 79
  • 1
  • 1
6

Check your imports, you should be using

java.net.HttpURLConnection

or

javax.net.ssl.HttpsURLConnection
DaveJohnston
  • 10,031
  • 10
  • 54
  • 83
5

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

4

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.

Mike Baranczak
  • 8,291
  • 8
  • 47
  • 71
4

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();
user120242
  • 14,918
  • 3
  • 38
  • 52
Geanluca
  • 41
  • 1
0

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.

AJC
  • 981
  • 10
  • 17