21

I'm writing a bit of code to upload a file from the device to the cloud over HTTPS.

Relevant snippet:

HttpsURLConnection conn = null; 
URL url = new URL(urlstring);
conn = (HttpsURLConnection) url.openConnection(); // exception here.

But the cast won't compile:

06-20 15:58:05.311: E/FNF(30286): java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection

I found this similar question: Using java class HttpsURLConnection, but I am not importing anything from the sun package.

My imports:

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import android.net.Uri;
import javax.net.ssl.HttpsURLConnection;
import android.util.Log;
import edu.mit.media.funf.storage.RemoteFileArchive;
import edu.mit.media.funf.util.LogUtil;

I've been scratching my head about this one for a while now, any suggestions?

1
  • It is a HttpURLConnection, you can't cast it to HttpsURLConnection. This is the same as the question you linked. You just using another incorrect class Commented Jun 20, 2012 at 20:24

5 Answers 5

88

Method 1: Your urlString must begin with https:// and not http:// for you to be able to cast it to a HttpsURLConnection.

Method 2: if your urlString starts with http://, changing HttpsURLConnection to HttpURLConnection should work

Sign up to request clarification or add additional context in comments.

3 Comments

as a comment, if your urlString starts with http://, changing HttpsURLConnection for HttpURLConnection solved the error :)
@MatiasElorriaga- your comment is more useful from the answer... thanks.
I have used java.net.HttpURLConnection for unscured URLs but Google would not accept HttpURLConnection implementation during publish application on play store. Alert message by Play store - "Your app(s) are using an unsafe implementation of the HostnameVerifier interface. " Please provide any solution ?
1

I had same Exception java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection

uri = new URL("http://www.google.com");
HttpsURLConnection connection = (HttpsURLConnection) uri.openConnection(); // Exception

I changed

uri = new URL("http://www.google.com");

to

uri = new URL("https://www.google.com");

Now it is working perfectly.

Comments

0

url.openConnection(); seems to be returning an object of type libcore.net.http.HttpURLConnectionImpl while you have declared your "conn" object as being of type import javax.net.ssl.HttpsURLConnection;. You need to sort up your imports and used objects. Maybe you missed something in the tutorial you were following.

Comments

0

Simple remove urlConnection.setDoOutput(true);

it will work fine.

Comments

0

url.openConnection() returns HttpURLConnection if your url is an HTTP request, it returns HttpsURLConnection if your url is an HTTPS request.

  • you can cast HttpsURLConnection to HttpURLConnection, but the inverse can lead to an exception if your url is an HTTP url, which you have seen.

  • so, to use HttpsURLConnection, you should always cast your url.openConnection() to HttpURLConnection, if you have tasks with HttpsURLConnection, example use your own X509TrustManager, you should check first if your URL is an HTTPS.

herein a sample code:

 HttpURLConnection conn = (HttpURLConnection) url.openConnection();

 if (url.get_protocol().equals("https")) {
      HttpsURLConnection sconn = (HttpsURLConnection)conn;

      /* do tasks with https connection */
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.