简体   繁体   English

使用原始套接字发送HTTP响应,客户端未收到消息

[英]Sending HTTP response with raw sockets, client doesn't receive message

I am trying to send this message entity body to the client. 我正在尝试将此消息实体正文发送给客户端。 But, this message seems to be not transmitted. 但是,此消息似乎未发送。 What possible reason is that results in this situation ? 导致这种情况的可能原因是什么? String message 字串讯息

String message = "HTTP/1.0 404 Not Found\r\n" +
                  "Server: " + 
                 "Content-type: html" +
                 "error\r\n" +
                 "<HTML>" +
                 "<HEAD><TITLE>404</TITLE></HEAD>" +
                 "<BODY>404 Not Found" +
                 "<br> nothing </BODY></HTML>" ;

Then, I have used PrintStream instace to send the message, its argument is message.getBytes() 然后,我使用PrintStream实例发送消息,其参数为message.getBytes()

The html message is not seen on the browser. 在浏览器中看不到html消息。

  PrintStream sender = new PrintStream(this.socket.getOutputStream());

  String message = // above lines

  sender.write(message.getBytes())

Assuming you are sending this data across via a raw socket, you need to fix the following: 假设您正在通过原始套接字发送此数据,则需要修复以下问题:

  • You need a CRLF (carriage return, line feed) after each header. 每个标题后面都需要一个CRLF(回车,换行)。
  • The header fields should not have the ':' character in them. 标头字段中不得包含“:”字符。
  • Content-type should be Content-Type , and there is no standard header named error (unless thats a custom one your sending, remove it). Content-type应该是Content-Type ,并且没有命名为error标准头(除非那是您发送的自定义项,请将其删除)。
  • html is not a valid content type, try text/html . html不是有效的内容类型,请尝试使用text/html

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

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