我正在尝试连接到 Rails 服务器,但我不断得到的响应是
Length Required WEBrick::HTTPStatus::LengthRequired
我正在使用 TCPSocket 连接到服务器。
require 'socket'
host = 'localhost'
port = 3000
path = '/books/show'
#Path of the controller and action to connect to
request = "POST #{path} HTTP/1.0\r\n\r\n "
socket = TCPSocket.open(host,port)
socket.print(request)
我怀疑它是指定内容长度的方式
socket.puts "content-length: 206\r\n"
#write response from server to html file
File.open('test2.html', 'w') do |res|
while (response_text = socket.gets)
res.puts "#{response_text}"
end
end
socket.close