gpt4 book ai didi

Ruby TCPSocket/HTTP 请求

转载 作者:数据小太阳 更新时间:2023-10-29 08:28:44 25 4
gpt4 key购买 nike

我刚开始使用 TCPSockets。我只是想获取谷歌主页。这是我的代码:

require 'socket'

host = 'http://www.google.com'
port = 80

s = TCPSocket.open host, port
s.puts "GET / HTTP/1.1\r\n"
s.puts "Host: Firefox"
s.puts "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
s.puts "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"
s.puts "\r\n"

while line = s.gets
puts line.chop
end


s.close

返回:

HTTP/1.1 302 Document has moved
Location: http://92.242.140.29/?nxdomain=http%3A%2F%2Ffirefox&AddInType=2&PlatformInfo=pbrgen

为什么?我的目标是获取谷歌主页的内容。谢谢

最佳答案

require 'socket'

host = 'www.google.com'
port = 80

s = TCPSocket.open host, port
s.puts "GET / HTTP/1.1\r\n"
s.puts "\r\n"

while line = s.gets
puts line.chop
end

s.close

此外,使用真正的 HTTP 客户端将使您的生活变得更加轻松。我喜欢Typhoeus .

关于Ruby TCPSocket/HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8649860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com