HTTP响应代码400将GET请求发送到HTTPS查询API

时间:2015-02-09 10:09:20

标签: java amazon-ses

我尝试使用SES HTTPS查询API发送电子邮件。我有一个java方法向Amazon SES端点发送GET请求,我试图发送带有SES的电子邮件并捕获结果。

代码:

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个响应。

1 个答案:

答案 0 :(得分: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);