TCPSocket을 사용하여 HTML 양식 게시물에서 콘텐츠 가져 오기
문제
TCPSOTOTE 객체에서 '가져 오기'메소드를 반복적으로 사용하면 다음과 같은 출력을받습니다
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
.
igets를 다시 사용하는 경우 모든 것이 매달려 있습니다. 어떻게 길이 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