Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uploading to Imgur v3 using Java https errors

I'm currently trying to upload to imgur using their current API v3, however I keep getting the error

error: javax.net.ssl.SSLException: hostname in certificate didn't match: api.imgur.com != imgur.com OR imgur.com

The error is pretty self-explaintory so I thought I would try using http instead but I get the error code 400 with imgur. I am not sure if this means how I am trying to upload is wrong or if Imgur doesn't like not SSL connections.

Below is my module of code connecting to Imgur:

public String Imgur (String imageDir, String clientID) {
    //create needed strings
    String address = "https://api.imgur.com/3/image";

    //Create HTTPClient and post
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(address);

    //create base64 image
    BufferedImage image = null;
    File file = new File(imageDir);

    try {
        //read image
        image = ImageIO.read(file);
        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        ImageIO.write(image, "png", byteArray);
        byte[] byteImage = byteArray.toByteArray();
        String dataImage = new Base64().encodeAsString(byteImage);

        //add header
        post.addHeader("Authorization", "Client-ID" + clientID);
        //add image
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("image", dataImage));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        //execute
        HttpResponse response = client.execute(post);

        //read response
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String all = null;

        //loop through response
        while (rd.readLine() != null) {
            all = all + " : " + rd.readLine(); 
        }

        return all;

    }
    catch (Exception e){
        return "error: " + e.toString();
    }
}

I hope someone can help in either finding the error in the above code or explaining how to fix the current HTTPS issue, thanks.

like image 977
Validus Avatar asked Oct 06 '22 03:10

Validus


1 Answers

It looks like the domain name in the certificate does not match the domain name that you are accessing, so SSL is failing as expected. You can tell HttpClient to ignore the certificate problem and just establish the connection. See this stackoverflow answer for details.

like image 184
Christian Trimble Avatar answered Oct 13 '22 10:10

Christian Trimble