简体   繁体   English

使用tcpsocket的tutorialspoint的简单Web浏览器

[英]tutorialspoint's simple web browser using tcpsocket

This piece of code supposedly gets the content of any web page: 这段代码应该可以获取任何网页的内容:

require 'socket'

host = 'www.tutorialspoint.com'     # The web server
port = 80                           # Default HTTP port
path = "/index.htm"                 # The file we want 

# This is the HTTP request we send to fetch a file
request = "GET #{path} HTTP/1.0\r\n\r\n"

socket = TCPSocket.open(host,port)  # Connect to server
socket.print(request)               # Send request
response = socket.read              # Read complete response
# Split response at first blank line into headers and body
headers,body = response.split("\r\n\r\n", 2) 
puts headers
puts body                          

When I run it in the command line, I get a 404 Error, but when i go to www.tutorialspoint.com/index.htm it's there, so what gives?: 当我在命令行中运行它时,出现404错误,但是当我访问www.tutorialspoint.com/index.htm时,它在那里,那有什么用?:

404 Error Information 404错误信息

Although, I don't have trouble using the open-uri library to get the contents of a web page. 虽然,我使用open-uri库获取网页内容没有问题。 But I want to know how to use this one though. 但是我想知道如何使用它。

Your request misses the Host parameter: 您的请求缺少Host参数:

host = 'www.tutorialspoint.com'     # The web server
port = 80                           # Default HTTP port
path = "/index.htm"                 # The file we want 

# This is the HTTP request we send to fetch a file
request = "GET #{path} HTTP/1.0\r\nHost: #{host}\r\n\r\n"

Note that apparently not all and every webserver require the "Host:" line (but see comments). 请注意,显然并非所有Web服务器都需要“ Host:”行(但请参见注释)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM