TransWikia.com

Can't issue an HTTP request to my Apache2 local server within my IDE

Ask Ubuntu Asked by Delio on November 2, 2020

I’ve just installed Apache2 on my Ubuntu machine and I’ve created a simple HTML (Hello World message) page within the dedicated directory for such content:

/var/www/html/example/index.html

I’m able to see the "Hello Word" message on my browser (Firefox). I used the common URL to perform this kind of operation:

http://127.0.0.1/example/index.html

It works well!

but when I’m trying to do the same request with some Java code it only return me a 400 error:

HTTP/1.1 400 Bad Request
Date: Wed, 21 Oct 2020 15:45:21 GMT
Server: Apache/2.4.41 (Ubuntu)
Content-Length: 301
Connection: close
Content-Type: text/html; charset=iso-8859-1

My Java code looks like this:

public static void main(String[] args) throws UnknownHostException, IOException {
          // The host and port to be connected.
      String host = "127.0.0.1";
      int port = 80;
      // Create a TCP socket and connect to the host:port.
      Socket socket = new Socket(host, port);
      // Create the input and output streams for the network socket.
      BufferedReader in
         = new BufferedReader(
              new InputStreamReader(socket.getInputStream()));
      PrintWriter out
         = new PrintWriter(socket.getOutputStream(), true);
      // Send request to the HTTP server.
      out.println("GET /example/index.html HTTP/1.1");
      out.println("Host: 127.0.0.1");
      out.println();   // blank line separating header & body
      out.flush();
      // Read the response and display on console.
      String line;
      // readLine() returns null if server close the network socket.
      while((line = in.readLine()) != null) {
         System.out.println(line);
      }
      // Close the I/O streams.
      in.close();
      out.close();

Can someone help me?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP