0

I need to create a Java application that creates a PUT request formatted like this:

PUT /AccountId/vaults/VaultName HTTP/1.1
Host: glacier.Region.amazonaws.com
Date: Date
Authorization: SignatureValue
Content-Length: Length
x-amz-glacier-version: 2012-06-01

I'm pretty new to formatting requests so any help would be appreciated.The only thing that I have in my code is:

public static void main(String[] args) {
        try {
            URL url = new URL("http://glacier.us-east-1.amazonaws.com");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("PUT");
            int code = con.getResponseCode();
            System.out.print(code);
        }

        catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

As you can tell I'm pretty lost.

Tyler
  • 23
  • 3
  • You may have a look at this answer from another question: https://stackoverflow.com/a/3283496/10241179. Also note that you are accessing to a resource that requires authorization, and maybe _basic authorization_ is not allowed in your case. You can read this other question about HTTP authorization using Java https://stackoverflow.com/questions/496651/connecting-to-remote-url-which-requires-authentication-using-java – Néstor Lucas Martínez Aug 27 '18 at 15:35
  • Ok thank you! I'll take a look at that! – Tyler Aug 27 '18 at 15:37

1 Answers1

0

You could use sockets, in order to write the request. In this way, you can write the request as you need, you just have to be careful about the line ending characters.

Please see the following example:

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

public class Test {

    public static void main(String[] args) {

        try {

            String hostname = "glacier.us-east-1.amazonaws.com";
            int port = 80;

            InetAddress addr = InetAddress.getByName(hostname);
            Socket socket = new Socket(addr, port);
            // Send headers
            BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
            wr.write("PUT /AccountId/vaults/VaultName HTTP/1.1\n\r");
            wr.write("Host: glacier.Region.amazonaws.com\n\r");
            wr.write("Date: Date\n\r");
            wr.write("Authorization: SignatureValue\n\r");
            wr.write("Content-Length: Length\n\r");
            wr.write("x-amz-glacier-version: 2012-06-01\n\r");
            wr.write("\n\r");
            wr.flush();

            // Get response
            BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                System.out.println(line);
            }

            wr.close();
            rd.close();

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

    }

}

With the above example, I get:

HTTP/1.1 500 Internal Server Error
Transfer-Encoding: chunked
Date: Mon, 27 Aug 2018 16:05:29 GMT
Connection: close

0

So, the request was sent "correctly". But something happened on the server.

I hope it helps you, bye.

Alessandro
  • 4,032
  • 4
  • 26
  • 59