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?

Community
  • 1
  • 1
Teddy
  • 1,996
  • 5
  • 21
  • 33
  • 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 – J-16 SDiZ Jun 20 '12 at 20:24

4 Answers4

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

nircraft
  • 8,242
  • 5
  • 30
  • 46
cklab
  • 3,761
  • 20
  • 29
  • 3
    as a comment, if your urlString starts with http://, changing HttpsURLConnection for HttpURLConnection solved the error :) – Matias Elorriaga Jul 29 '15 at 18:00
  • @MatiasElorriaga- your comment is more useful from the answer... thanks. – offset Nov 02 '15 at 11:31
  • 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 ? – Praveen Kumar Verma Mar 06 '17 at 09:29
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.

anoopknr
  • 3,177
  • 2
  • 23
  • 33
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.

Morfic
  • 15,178
  • 3
  • 51
  • 61
0

Simple remove urlConnection.setDoOutput(true);

it will work fine.

Nazik
  • 8,696
  • 27
  • 77
  • 123
Mediasoft
  • 271
  • 2
  • 7
  • 18