使用TCPSocket从HTML表单帖子中获取内容
题
在TCPSOcket对象中反复使用“获取”方法,我收到以下输出
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
.
如果我再次使用.gets一切挂起,我将如何获得长度45的“内容”?我可以从我的tcpsocket对象中得到它吗?我必须使用别的东西吗?
解决方案
gets
reads a line, so it will block until the connection times out or it reads a newline character. You should use the read
method, which reads a given number of bytes (which is very conveniently provided in the Content-Length header).
其他提示
Include the header:
Connection: close
to the header. This will prevent the hanging before actually reading the body of the request.
不隶属于 StackOverflow