2015-02-09 129 views
0

我試圖使用SES HTTPS查詢API發送電子郵件。我有一個發送GET請求到Amazon SES端點的java方法,我試圖用SES發送郵件並捕獲結果。HTTP響應代碼400向HTTPS查詢API發送GET請求

代碼:

public static String SendElasticEmail(String timeConv,String action,String source, String destinationAddr, String subject, String body) { 
    try { 
     System.out.println("date : "+timeConv); 

     System.out.println("In Sending Mail Method......!!!!!"); 

     //Construct the data 
     String data = "Action=" + URLEncoder.encode(action, "UTF-8"); 
     data += "&Source=" + URLEncoder.encode(source, "UTF-8"); 
     data += "&Destination.ToAddresses.member.1=" + URLEncoder.encode(destinationAddr, "UTF-8"); 
     data += "&Message.Subject.Data=" + URLEncoder.encode(subject, "UTF-8"); 
     data += "&Message.Body.Text.Data=" + URLEncoder.encode(body, "UTF-8"); 

     //Send data 
     System.out.println("https://email.us-east-1.amazonaws.com?"+data); 
     URL url = new URL("https://email.us-east-1.amazonaws.com?"+data); 
     //URLConnection conn = url.openConnection(); 

     HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); 

     con.setRequestMethod("GET"); 

     con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
     con.setRequestProperty("x-amz-date" , timeConv); 
     con.setRequestProperty("Content-Length", ""+data.toString().length()); 

     con.setRequestProperty("X-Amzn-Authorization" , authHeader); 

     int responseCode = ((HttpsURLConnection) con).getResponseCode(); 
     String responseMessage = ((HttpsURLConnection) con).getResponseMessage(); 

     System.out.println("\nSending 'GET' request to URL : " + url); 
     System.out.println("Response Code : " + responseCode); 
     //System.out.println("Response Message : " + responseMessage); 

     InputStream stream = con.getInputStream(); 
     InputStreamReader isReader = new InputStreamReader(stream); 

     System.out.println("hgfhfhfhgfgfghfgh"); 
     BufferedReader br = new BufferedReader(isReader); 
     String result = ""; 
     String line; 
     while ((line = br.readLine()) != null) { 
      result+= line; 
     } 
     System.out.println(result); 
     br.close(); 
     con.disconnect(); 
    } 

    catch(Exception e) { 
     e.printStackTrace(); 
    } 
    return subject; 
} 

我已經計算出正確的簽名,因爲從郵遞員客戶打200得到響應。

+0

使用AWS SDK for Java會更容易。 – timrau 2015-02-09 10:17:42

回答

0
URL url = new URL("https://email.us-east-1.amazonaws.com?"+data); 

您錯過了問號前的'/'。它應該是

URL url = new URL("https://email.us-east-1.amazonaws.com/?"+data); 
+0

supperrb答案.......你救了我的命....... – 2015-02-09 10:55:12