0

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  
4

1 に答える 1

0

ヘッダーは空白行で終了し、コンテンツ長のバイトを書き込む必要があります。次のようなことを試してください (2 番目の \r\n の移動と 206 個のスペースの配置に注意してください)。

require 'socket'
host = 'localhost'
port = 3000
path = '/products'
#Path of the controller and action to connect to
request = "POST #{path} HTTP/1.0\r\n"
socket = TCPSocket.open(host,port)
socket.print(request)
socket.puts "content-length: 206\r\n\r\n"
socket.puts ' ' * 206
#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
于 2013-04-14T22:58:18.427 に答える