2013-02-05 32 views
5

我目前正在試圖上傳使用他們目前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,感謝幫助。

+0

你有沒有找到答案?我真的需要弄清楚如何做到這一點,並且創建我自己的SSLFactory也不起作用。 –

回答

4

它看起來像證書中的域名與您正在訪問的域名不匹配,所以SSL按預期失敗。您可以告訴HttpClient忽略證書問題並建立連接。有關詳細信息,請參閱此stackoverflow answer

+0

這確實是問題所在,但我使用這裏的答案解決了它:http://stackoverflow.com/questions/3135679/android-httpclient-hostname-in-certificate-didnt-match-example-com-ex 。我現在不會讚賞這個獎金,但會給予讚賞。 –