1

I am following the "4. Java Socket Client Example: a HTTP Client" instruction from https://www.codejava.net/java-se/networking/java-socket-client-examples-tcp-ip in my Mac using IntelliJ.

The Http config is as easy as:

            PrintWriter writer = new PrintWriter(output, true);

            writer.println("HEAD " + url.getPath() + " HTTP/1.1");
            writer.println("Host: " + hostname);
            writer.println("User-Agent: Simple Http Client");
            writer.println("Accept: text/html");
            writer.println("Accept-Language: en-US");
            writer.println("Connection: close");
            writer.println();

I copied the code without any change in the IntelliJ to test how would it work. However, after I did "java HttpClient.java" and "java HttpClient http://www.codejava.net/java-core" as indicated, what I got is:

HTTP/1.1 400 Bad Request
Date: Mon, 04 May 2020 07:51:30 GMT
Server: Apache
Content-Length: 420
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<p>Additionally, a 400 Bad Request
error was encountered while trying to use an ErrorDocument to handle the request.</p>
<hr>
<address>Apache Server at gator3107.hostgator.com Port 80</address>
</body></html>

I tried many solutions but none of them works for me. The only problem I found is that the HttpClient.class compiled with java version 11 missed lines of

writer.println("HEAD " + url.getPath() + " HTTP/1.1");
writer.println("Host: " + hostname);

then I changed java version to 1.8 it added the missing lines, but the error did not change. The interesting thing is, one of my friend doing the same thing in windows got everything as expected.

Any help would be appreciated.

1 Answer 1

1

The issue is how new lines are printed on Windows and Mac, Windows treats new lines as 2 characters, CR - Carriage return ("\r") + LF- Line feed ("\n") "\r\n", Mac prints new lines as LF("\n") only. HTTP requests expects each line to be separated by CRLF "\r\n", what your code is printing is just "\n" on Mac and "\r\n" on Windows that is why it works as expected on Windows platform.

To make it work on both Windows and Mac try the following code:

            PrintWriter writer = new PrintWriter(output, true);

        writer.print("HEAD " + url.getPath() + " HTTP/1.1\r\n");
        writer.print("Host: " + hostname+"\r\n");
        writer.print("User-Agent: Simple Http Client\r\n");
        writer.print("Accept: text/html\r\n");
        writer.print("Accept-Language: en-US\r\n");
        writer.print("Connection: close\r\n");
        writer.print("\r\n");
Sign up to request clarification or add additional context in comments.

2 Comments

Oh Gosh!!! Many thanks, this fixed it finally! Hhh. But it seems like adding an \r after each line instead of \r\n can make it work on Mac as you said Mac would print a \n by itself.
Yes, but it would only work on Mac, if you were to run that same program on Windows it would print "\r\r\n"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.