Ruby TCPSocket / HTTP request

萝らか妹 提交于 2019-12-06 13:24:53
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

Also, using a real HTTP client will make your life much, much easier. I like Typhoeus.

sczizzo

A 302 status is a type of HTTP redirect, but here you're working with TCP, a network layer below HTTP, which doesn't understand redirects (or anything else HTTP). As this SO post shows, howerver, there are other ways to request a web page, namely using the OpenURI library instead of sockets.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!