我目前正在尝试使用他们当前的 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 问题,谢谢。
证书中的域名似乎与您正在访问的域名不匹配,因此 SSL 按预期失败。您可以告诉 HttpClient 忽略证书问题并仅建立连接。看到这个堆栈溢出答案 https://stackoverflow.com/a/2703233/1818625了解详情。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)