uploading to Imgur v3 using Java https errors
Asked Answered
C

1

5

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.

Copyright answered 5/2, 2013 at 14:48 Comment(1)
Did you ever find an answer to this? I really need to figure out how to do this, and creating my own SSLFactory didn't work either.Intertidal
H
4

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.

Hanukkah answered 6/4, 2013 at 0:18 Comment(1)
This did turn out to be the problem, but I solved it using the answer here: #3136179. I'll refrain the bounty for now, but will give an upvote.Intertidal

© 2022 - 2024 — McMap. All rights reserved.