2013-04-14 9 views
0

나는 레일 서버에 연결하려고하지만 난 점점 계속 응답 내가 서버에 연결 TCPSocket을 사용하고TCPSocket을 사용하여 Ruby를 통해 Rails 서버에 연결하십시오. "길이가 필요합니다. WEBrick :: HTTPStatus :: LengthRequired"를 얻습니다. 어떤 도움을 주시겠습니까?

Length Required WEBrick::HTTPStatus::LengthRequired 

입니다.

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 

어떤 도움 지정되어 있는지 그 방법을 생각?

답변

0

빈 줄은 헤더를 끝내고 내용 길이 바이트를 써야합니다. 다음과 같은 것을 시도해보십시오 (두 번째 \ 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 
+0

고맙습니다 @SamRuby, 바로 가리 킵니다. –