使用 Java ServerSocket 读取 TCP 端口上的 HTTP 参数

Read HTTP Parameters on TCP port using Java ServerSocket

如果我从 Java Servlet 发送 HTTP 请求并使用 ServerSocket 在 TCP 端口上接收它,我将如何读取 HTTP 请求参数。谁能帮我解决这个问题?

以下是我的设计

Servlet

GET/POST 使用 HttpURLConnection

    URL url = new URL("http://localhost:2309/");
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setRequestProperty("Content-Type", "application/text");
    connection.setRequestProperty("Accept", "application/text");
    connection.setRequestMethod("POST");        
    connection.setRequestProperty("header1", "value1");

at localhost : 2309 我有一个 ServerSocket 正在侦听来自上面的 servlet 的请求。我正在尝试读取请求,但我只读取了 HTTP Headers,但我没有看到请求参数(我知道在上面的示例中我没有发送任何参数,我已经通过获取连接并写入它)。

这就是我尝试将请求参数发送到我的 ServerSocket 程序的方式。

byte[] parameters = someString.getBytes();
        OutputStream outStream = connection.getOutputStream();
        outStream.write(parameters);

以下是我的ServerSocket程序。

public static void main(String... args) {
    int port = 2309;
    ServerSocket sSocket = new sSocket(port);
    System.out.println("### SERVER IS UP AND RUNNING, WAITING FOR A CLIENT TO CONNECT ON " + port + " ###");
    Socket cSocket = sSocket.accept();
    System.out.println("### CONNECTION WITH THE CLIENT CREATED ###");
    BufferedReader readRequest = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));
    PrintWriter writeResponse = new PrintWriter(cSocket.getOutputStream());
    String line = "";
    while (readRequest != null  && (line = readRequest.readLine()) != null) {
        if (line.length() == 0)
            break;
        System.out.println(line);
    }
    writeResponse.write("HTTP/1.0 200 OK\r\n");
    writeResponse.write("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n");
    writeResponse.write("Server: Apache/0.8.4\r\n");
    writeResponse.write("Content-Type: text/html\r\n");
    writeResponse.write("Content-Length: 59\r\n");
    writeResponse.write("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n");
    writeResponse.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n");
    writeResponse.write("\r\n");
    writeResponse.write("<TITLE>Example</TITLE>");
    writeResponse.write("<P>This is an example</P>");
}

以下是我在 ServerSocket 程序输出中看到的内容。

### SERVER IS UP AND RUNNING, WAITING FOR A CLIENT TO CONNECT ON 2309 ###
### CONNECTION WITH THE CLIENT CREATED ###
POST / HTTP/1.1
Content-Type: application/text
Accept: application/text
header1: value1
Cache-Control: no-cache
Pragma: no-cache
User-Agent: Java/1.7.0_75
Host: localhost:2309
Connection: keep-alive
### CONNECTION WITH THE CLIENT TERMINATED ###

谁能推荐我

  1. 如何读取请求参数

  2. 在连接 object 上写入输出流,它会让我得到 请求参数就在这个地方?

  3. 当我只想保持独立时,这是一个好方法吗? 服务器启动,这将只是听取即将到来的请求 单个端口并为其提供服务[或]有更好的方法 我能做到吗?

我无法从 HTTP 负载中获取正文。可能的原因是在线下。

BufferedReader readRequest = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));

我改成了

    public static void main(String... args) {
    int port = 2309;
    sSocket sSocket = new sSocket(port);
    System.out.println("### SERVER IS UP AND RUNNING, WAITING FOR A CLIENT TO CONNECT ON " + port + " ###");
    Socket cSocket = sSocket.accept();
    System.out.println("### CONNECTION WITH THE CLIENT CREATED ###");
    InputStream readRequest = cSocket.getInputStream();
    PrintWriter writeResponse = new PrintWriter(cSocket.getOutputStream());
    byte[] buf = new byte[4096];
    readRequest.read(buf);
    String httpPayload = new String(buf, "UTF-8");
    HttpPayload httpPayloadObject = new HttpPayload(httpPayload);
    Map<String, Object> httpParameters = httpPayloadObject.getHttpPayloadBodyMap();
    PushNotificationEvent event = new PushNotificationEvent(httpParameters);
    event.processEvent();
    writeResponse.write("HTTP/1.0 200 OK\r\n");
    writeResponse.write("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n");
    writeResponse.write("Server: Apache/0.8.4\r\n");
    writeResponse.write("Content-Type: text/html\r\n");
    writeResponse.write("Content-Length: 59\r\n");
    writeResponse.write("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n");
    writeResponse.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n");
    writeResponse.write("\r\n");
    writeResponse.write("<TITLE>Example</TITLE>");
    writeResponse.write("<P>This is an example</P>");
}

缓冲输入流省略了主体部分。现在我得到了想要的输出

POST / HTTP/1.1
Host: 127.0.0.1:2309
Connection: keep-alive
Content-Length: 63
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
header1: value1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW6**strong text**4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip, deflate
    en-US,en;q=0.8

{jsondata : {key1:value1_getting_bigger_content,  key2:value2}}

之前没有出现在有效载荷中的部分是

{jsondata : {key1:value1_getting_bigger_content,  key2:value2}}

我仍然不确定为什么 BufferedReader 或 BufferedInputStream 会省略 body

您忽略了提供的 content-length 并尝试读取直到流结束,这永远不会到达,因为客户端尚未关闭连接,因为他正在尝试读取响应。当您在 header 之后读取 content-length 个字节时,如果 header 存在,则需要停止阅读。

注意如果您使用的是 Reader,您应该使用在 header 中发送的相同字符集构建它。