使用Java https错误上传到Imgur v3 [英] uploading to Imgur v3 using Java https errors

查看:118
本文介绍了使用Java https错误上传到Imgur v3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用他们当前的API v3上传到imgur,但是我一直收到错误

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


错误:javax。 net.ssl.SSLException:证书中的主机名不匹配:api.imgur.com!= imgur.com或imgur.com

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

错误是非常自我解释,所以我想我会尝试使用http而不是我得到错误代码400与imgur。我不确定这是否意味着我尝试上传是错误的还是Imgur不喜欢SSL连接。

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.

以下是我连接到Imgur的代码模块:

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();
    }
}

我希望有人可以帮助找到错误上面的代码或解释如何修复当前的HTTPS问题,谢谢。

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

推荐答案

看起来证书中的域名确实如此与您正在访问的域名不匹配,因此SSL按预期失败。您可以告诉HttpClient忽略证书问题并建立连接。有关详细信息,请参阅此 stackoverflow答案

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.

这篇关于使用Java https错误上传到Imgur v3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆