2015-02-09 147 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

相关问题