简体   繁体   English

400 来自 Http 请求的错误请求使用 java

[英]400 Bad request from Http request using java

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.我正在遵循https://www.code-java.net/lijj-se/cp-ip-ip-java-java.net/lij-se/tnetworking中的 https://www.code-java-net/lij-se/cp-ip-my-socket-client .

The Http config is as easy as: 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();

I copied the code without any change in the IntelliJ to test how would it work.我在 IntelliJ 中没有任何更改就复制了代码来测试它是如何工作的。 However, after I did "java HttpClient.java" and "java HttpClient http://www.codejava.net/java-core " as indicated, what I got is:但是,在我按照指示执行“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>

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我发现的唯一问题是使用 java 版本 11 编译的 HttpClient.class 缺少行

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.然后我将 java 版本更改为 1.8 它添加了缺少的行,但错误没有改变。 The interesting thing is, one of my friend doing the same thing in windows got everything as expected.有趣的是,我的一位朋友在 windows 中做同样的事情得到了预期的一切。

Any help would be appreciated.任何帮助,将不胜感激。

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.问题是如何在 Windows 和 Mac 上打印新行,Windows 将新行视为 2 个字符,CR - 回车 ("\r") + LF- 换行 ("\n") "\r\n" , Mac仅将新行打印为LF("\n") 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. HTTP 请求期望每行由 CRLF "\r\n"分隔,您的代码打印的只是 Mac 上的"\n" \n" 和 Windows 上的"\r\n"这就是它在 ZAEA234289CE3AA9B43EZEB 平台上按预期工作的原因。

To make it work on both Windows and Mac try the following code:要使其在 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM