【问题标题】:uploading to Imgur v3 using Java https errors使用 Java https 错误上传到 Imgur v3
【发布时间】:2013-02-05 14:48:22
【问题描述】:

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

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

这个错误是不言自明的,所以我想我会尝试使用 http,但我使用 imgur 得到错误代码 400。我不确定这是否意味着我尝试上传的方式是错误的,或者 Imgur 不喜欢 SSL 连接。

下面是我连接到 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 问题,谢谢。

【问题讨论】:

  • 你找到答案了吗?我真的需要弄清楚如何做到这一点,并且创建自己的 SSLFactory 也不起作用。

标签: java post https imgur


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 2010-12-21
  • 2014-04-28
  • 1970-01-01
  • 1970-01-01
  • 2013-06-20
  • 1970-01-01
  • 2014-12-08
  • 2012-03-04
  • 1970-01-01
相关资源
最近更新 更多