连接使用HttpUrlConnection连接Amazon S3时拒绝连接异常

时间:2013-02-28 09:20:52

标签: google-app-engine gwt amazon-s3 httpurlconnection

我正在使用gwt 2.5,GAE 1.7.4。我正在尝试连接Amazon S3并上传文件,但我收到Connection Refuse错误。我没有找到亚马逊教程的任何解决方案。我的代码如下:

try{

        URL url = new URL("https://temptxt.s3-website-us-east-1.amazonaws.com");

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);

        String userName = "MyAccessKey".trim();
        String password = "MySecretKey".trim();

        String authString = userName + ":" + password;
        String authStringBase64 = Base64.encodeBase64String(authString.getBytes());
        authStringBase64 = authStringBase64.trim();

        connection.setRequestProperty("Content-type", "text/plain");
        connection.setRequestProperty("Authorization", "AWS" + authStringBase64);

        int responseCode = connection.getResponseCode();
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        String decodedString;
        while ((decodedString = in.readLine()) != null) {
            System.out.println("Read Data From Response : " + decodedString);
        }
        in.close();

        if (responseCode == 401) {
            throw new IllegalStateException("Error 401");
        }
    }catch (Exception e) {
        System.out.println("Error : "+e);
    }

0 个答案:

没有答案