2014-01-24 28 views
0

我設法讓代理服務器運行。 目前它只發送硬編碼的GET請求,但暫時沒問題。 問題是我不知道如何讓我從網絡服務器收到的數據到 顯示在我的瀏覽器中。但是,我可以在終端打印數據的正文,而不會出現任何問題。 一個不相關的問題是,返回的數據是「302 Moved」錯誤。 我非常感謝任何幫助!如何讓我的本地代理服務器將html返回給瀏覽器?

require 'socket' 

def handle_request(client, host, port, path) 
    puts "0" 
    socket = TCPSocket.open(host, port) 
    request = "GET #{path} HTTP/1.0\r\n\r\n" 

    socket = TCPSocket.open(host,port) 
    socket.print(request) 
    response = socket.read 

    headers,body = response.split("\r\n\r\n", 2) 
    client.puts body 
    puts body 

    socket.close 
end 



server = TCPServer.open(2000) 
loop{ 
    client = server.accept 
    host = "www.google.se" 
    port = 80 
    path = "/index.html" 
    handle_request(client, host, port, path) 

    client.close 
} 

回答

0

只要運行您的程序並將您的瀏覽器指向http://localhost:2000即可。

相關問題