0

This is only an exercise. I'm wondering if there is an equivalent of this code:

String https_url = "https://api.ciscospark.com/v1/rooms";
URL url = new URL(https_url);
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();

connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); 
connection.setRequestProperty("Authorization", "I3MDUtMmEy");
connection.setDoOutput(true);

I thought that could be possible to make something like:

String https_url = "https://api.ciscospark.com/v1/rooms";
        URL url = new URL(https_url);
        HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
        connection.setDoOutput(true);
        PrintWriter pw = new PrintWriter(connection.getOutputStream());
        pw.println("GET /v1/rooms HTTP/1.1");
        pw.println("Host: https://api.ciscospark.com");
        pw.println("Content-Type: application/json; charset=utf-8");
        pw.println("Authorization: I3MDUtMmEy");
        pw.println("");

but I receive auth error message from Server returned HTTP response code: 401 for URL: https://api.ciscospark.com/v1/rooms 401 Authentication credentials were missing or incorrect. Can I use printwriter to make it work? And if not, why? thanks

sefiroths
  • 1,555
  • 3
  • 14
  • 29
  • If you are writing the HTTP request manually you can't use HttpsURLConnection (your code writes a request into the HTTP body where the server does not parse it). Use a raw TCP Socket instead. – Robert Nov 03 '18 at 15:15
  • if I use a raw TCP Socket should I implement manually the handshake? (I think) – sefiroths Nov 03 '18 at 20:31
  • What handshake? If you use a Socket/SSLSocket you are getting a TCP connection on which you get the InputStream/OutputStream. – Robert Nov 04 '18 at 11:34
  • I tryed with final SocketFactory socketFactory = SSLSocketFactory.getDefault(); String ipAddr = InetAddress.getByName("https://api.ciscospark.com/v1/rooms").toString(); but gives me java.net.UnknownHostException: https://api.ciscospark.com/v1/rooms: unknown error...and I'm not able to retrive ip even with nskookup... if I make Socket socket = socketFactory.createSocket("api.ciscospark.com/v1/rooms", 443)) says unknown host – sefiroths Nov 04 '18 at 14:00
  • Only use the hostname not the complete url (just the part before `/`). – Robert Nov 04 '18 at 14:09

1 Answers1

0

I have tryed with:

    private static void useSslSocket() throws IOException {
        final SocketFactory socketFactory = SSLSocketFactory.getDefault();
        String ipAddr = InetAddress.getByName("api.ciscospark.com").toString();
        System.out.println("IP: "+ipAddr);
        try (final Socket socket = socketFactory.createSocket("api.ciscospark.com", 80)) {
            final String request = "GET / HTTP/1.1\r\nConnection: close\r\nHost:stackoverflow.com\r\n\r\n";

            PrintWriter pw = new PrintWriter(socket.getOutputStream());



            pw.println("GET /v1/rooms HTTP/1.1");
            pw.println("Host: https://api.ciscospark.com");
            pw.println("Content-Type: application/json; charset=utf-8");
            pw.println("Authorization: I3MDUtMmEy");
            pw.println("");
//          pw.flush();
//          pw.close();

            final InputStream inputStream = socket.getInputStream();
            final String response = readAsString(inputStream);
            System.out.println(response);
        }
    }

response javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

sefiroths
  • 1,555
  • 3
  • 14
  • 29