【问题标题】:Why am I getting a HTTP 400 bad request response when sending a manually-crafted HTTP request over a TCP socket?为什么通过 TCP 套接字发送手动制作的 HTTP 请求时收到 HTTP 400 错误请求响应?
【发布时间】:2014-03-27 13:03:18
【问题描述】:

我正在尝试手动构建一个 HTTP 请求(作为一个字符串)并通过 TCP 套接字发送它,这就是我想要做的:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * SimpleHttpClient.java (UTF-8)
 *
 * Mar 27, 2014
 *
 * @author tarrsalah.org
 */
public class SimpleHttpClient {
    private static final String StackOverflow = "http://stackoverflow.com/";

    public static void main(String[] args) {

//      if (args.length < 1) {
//          System.out.println("Usage : SimpleHttpClient <url>");
//          return;
//      }

        try {
            URL url = new URL(StackOverflow);
            String host = url.getHost();
            String path = url.getPath();
            int port = url.getPort();
            if (port < 80) {
                port = 80;
            }

            //Construct and send the HTTP request
            String request = "GET" + path + "HTTP/1.1\n";
            request += "host: " + host;
            request += "\n\n";

            // Open a TCP connection
            Socket socket  = new Socket(host, port);
            // Send the request over the socket
            PrintWriter writer = new PrintWriter(socket.getOutputStream());
            writer.print(request);
            writer.flush();
            // Read the response
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String next_record = null;
            while ((next_record = reader.readLine()) != null) {
                System.out.println(next_record);
            }
            socket.close();
        } catch (MalformedURLException ex) {
            Logger.getLogger(SimpleHttpClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SimpleHttpClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

但无论选择哪个 URL,我都会收到此响应消息:

HTTP/1.0 400 Bad request
Cache-Control: no-cache
Connection: close
Content-Type: text/html

<html><body><h1>400 Bad request</h1>
Your browser sent an invalid request.
</body></html>

为什么?

【问题讨论】:

标签: java http httprequest http-status-code-400


【解决方案1】:

感谢Julian Reschke 的建议,我缺少两个空格字符(HTTP 动词后面的一个,路径后面的第二个)

String request = "GET " + path + " HTTP/1.1\n";
//                   ^            ^

【讨论】:

    【解决方案2】:

    我实际上有点不确定这是否能解决您的问题,但如果您阅读RFC2616, section 5,您会注意到它在Request-line 和标题之后非常明确地提到了CRLF,因此您可能会遗漏一些@ 987654324@'s 在您的代码中。

    干杯,

    【讨论】:

    • 这会导致 400 吗?
    猜你喜欢
    • 2012-05-27
    • 2012-06-03
    • 1970-01-01
    • 2012-10-04
    • 2013-01-26
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 2019-05-01
    相关资源
    最近更新 更多