400错误,来自使用Java的HTTP请求。

huangapple go评论85阅读模式
英文:

400 Bad request from Http request using java

问题

我正在按照"https://www.codejava.net/java-se/networking/java-socket-client-examples-tcp-ip"上的"4. Java Socket Client Example: a HTTP Client"指示,在我的Mac上使用IntelliJ进行操作。

Http配置非常简单:

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();

我在IntelliJ中未作任何更改即复制了代码,以测试它的工作原理。然而,在我执行了"java HttpClient.java"和"java HttpClient http://www.codejava.net/java-core"后,得到的结果是:

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>

我尝试了许多解决方案,但没有一个对我有效。唯一发现的问题是,使用Java 11编译的HttpClient.class丢失了这两行:

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

然后,我将Java版本更改为1.8,它添加了丢失的行,但错误没有改变。有趣的是,我有一个在Windows上进行相同操作的朋友,结果一切正常。

非常感谢您的帮助。

英文:

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(&quot;HEAD &quot; + url.getPath() + &quot; HTTP/1.1&quot;);
            writer.println(&quot;Host: &quot; + hostname);
            writer.println(&quot;User-Agent: Simple Http Client&quot;);
            writer.println(&quot;Accept: text/html&quot;);
            writer.println(&quot;Accept-Language: en-US&quot;);
            writer.println(&quot;Connection: close&quot;);
            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

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

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(&quot;HEAD &quot; + url.getPath() + &quot; HTTP/1.1&quot;);
writer.println(&quot;Host: &quot; + 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

得分: 1

问题在于在Windows和Mac上如何打印新行。Windows将新行视为2个字符,CR - 回车(“\ r”)+ LF- 换行(“\ n”)“\ r \ n”,Mac仅打印换行符(“\ n”)“\ n”。 HTTP请求希望每行由CRLF “\ r \ n”分隔,您的代码打印在Mac上只是“\ n”,在Windows上是“\ r \ n”,这就是为什么在Windows平台上正常工作的原因。

要使其在Windows和Mac上正常工作,请尝试以下代码:

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");
英文:

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") &quot;\r\n&quot;, Mac prints new lines as LF(&quot;\n&quot;) only. HTTP requests expects each line to be separated by CRLF &quot;\r\n&quot;, what your code is printing is just &quot;\n&quot; on Mac and &quot;\r\n&quot; 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(&quot;HEAD &quot; + url.getPath() + &quot; HTTP/1.1\r\n&quot;);
        writer.print(&quot;Host: &quot; + hostname+&quot;\r\n&quot;);
        writer.print(&quot;User-Agent: Simple Http Client\r\n&quot;);
        writer.print(&quot;Accept: text/html\r\n&quot;);
        writer.print(&quot;Accept-Language: en-US\r\n&quot;);
        writer.print(&quot;Connection: close\r\n&quot;);
        writer.print(&quot;\r\n&quot;);

huangapple
  • 本文由 发表于 2020年5月4日 16:25:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/61587986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定