RADXack
RADXack

Reputation: 1092

javax.microedition.pki Certificate Failed Verification

I'm trying to read in a JSON reply from the Google Sheets API in a Java ME MIDP application. I've tried the following with other addresses and it receives their content fine but the actual API I want to use is Sheets and it always returns an "Certificate Failed Verification" exception.

HttpConnection c = null;
InputStream is = null;
StringBuffer str = new StringBuffer();
try
    {

     c = (HttpsConnection)Connector.open(urlstring);
     c.setRequestMethod(HttpConnection.GET);
     c.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
     c.setRequestProperty("User-Agent","Profile/MIDP-2.1 Configuration/CLDC-1.1");

     is = c.openInputStream();

     int len = (int)c.getLength();
     int ch;

     while ( (ch = is.read() ) != -1)
         {
         str.append((char)ch);
         }


    }

catch( Exception e ){ alert( ""+e ); }

return str.toString();

Connector.open() implicitly returns a HttpsConnection if the URL starts with Https so it should still work.

An example of a HTTPS request

https://jsonplaceholder.typicode.com/posts/1

Which won't work but the above also allows for HTTP connections

http://jsonplaceholder.typicode.com/posts/1

Which will work.

Google Sheets however requires HTTPS and thus is not obtainable via the above code. How can I make a GET request over HTTPS to the sheets API? Thank you.

Upvotes: 1

Views: 206

Answers (2)

RADXack
RADXack

Reputation: 1092

From what I've gathered it seems that when connecting over HTTPS the phone uses an older version of SSL or TLS which has since been deprecated causing some API's to not respond.

I found that if you make an API request over HTTPs with the Opera Mini web browser it works. Giving you the desired response but on closer inspection it seems Opera gets the response for you and gives it back via a different URL. In attempt to furnish these older devices that cannot use a newer version of SSL/TLS to make the secure connection themselves.

Upvotes: 0

mr_lou
mr_lou

Reputation: 1920

I had a similar problem when implementing an online highscore system for one of our games. It would fetch highscores fine on some phones but didn't work on other phones. The explanation: Some phones have their own built-in "MIME-type checker". When you call (HttpConnection)Connector.open(urlstring) the phone expects a text/html response. When it instead gets a application/json (or other) response, the phone gives its own "Not found" error. Not sure if your problem is related, but worth a try? See if you can add a mime-type "application/json" in the request header of the HttpConnection.

Upvotes: 1

Related Questions