简体   繁体   English

使用TCPSocket从html表单POST获取内容

[英]getting the content from an html form POST using TCPSocket

using the 'gets' method repeatedly on a TCPSocket object, I receive the following output 在TCPSocket对象上重复使用'gets'方法,我收到以下输出

POST /targetPage.html HTTP/1.1
Host: 127.0.0.1:7125
...
...
...
Keep-Alive: 115
DNT: 1
Connection: keep-alive
Referer: http://127.0.0.1:7125/
Content-Type: application/x-www-form-urlencoded
Content-Length: 45

If I use .gets again everything hangs, how would I get this 'content' of length 45? 如果我再次使用.gets,那么我将如何获得长度为45的“内容”? Can I get this from my TCPSocket object or do I have to use something else? 我可以从TCPSocket对象中获取此信息,还是必须使用其他内容?

gets reads a line, so it will block until the connection times out or it reads a newline character. gets读取一行,因此它将阻塞,直到连接超时或读取换行符。 You should use the read method, which reads a given number of bytes (which is very conveniently provided in the Content-Length header). 您应该使用read方法,该方法读取给定数量的字节(在Content-Length标头中非常方便地提供)。

Include the header: 包括标题:

Connection: close 连接:关闭

to the header. 到标题。 This will prevent the hanging before actually reading the body of the request. 这样可以防止在实际读取请求正文之前挂起。

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

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