![](/img/trans.png)
[英]simplest way to send raw Byte-arrays using Ruby's TCPSocket-Class
[英]tutorialspoint's simple web browser using tcpsocket
这段代码应该可以获取任何网页的内容:
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
当我在命令行中运行它时,出现404错误,但是当我访问www.tutorialspoint.com/index.htm时,它在那里,那有什么用?:
虽然,我使用open-uri库获取网页内容没有问题。 但是我想知道如何使用它。
您的请求缺少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"
请注意,显然并非所有Web服务器都需要“ Host:”行(但请参见注释)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.