我目前正在試圖上傳使用他們目前API V3到imgur,但我不斷收到錯誤上傳到Imgur V3使用Java HTTPS錯誤
error: javax.net.ssl.SSLException: hostname in certificate didn't match: api.imgur.com != imgur.com OR imgur.com
錯誤是相當自我explaintory所以我想我會嘗試使用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也不起作用。 –