为什么Ruby Net::HTTP.get\u响应和Net::HTTP.new(uri.host).request返回不同的东西?
这两个请求的结果应该相同,但第一个请求返回200(OK),第二个请求返回404(NotFound)。为什么呢为什么Ruby Net::HTTP.get\u响应和Net::HTTP.new(uri.host).request返回不同的东西?,ruby,net-http,Ruby,Net Http,这两个请求的结果应该相同,但第一个请求返回200(OK),第二个请求返回404(NotFound)。为什么呢 require 'net/http' url = "http://readwrite.com/2013/12/04/google-compute-engine" uri = URI(url) Net::HTTP.get_response(uri) #=> #<Net::HTTPOK 200 OK readbody=true> Net::HTTP.new(uri.hos
require 'net/http'
url = "http://readwrite.com/2013/12/04/google-compute-engine"
uri = URI(url)
Net::HTTP.get_response(uri)
#=> #<Net::HTTPOK 200 OK readbody=true>
Net::HTTP.new(uri.host).request(Net::HTTP::Get.new(url))
#=> #<Net::HTTPNotFound 404 Not Found readbody=true>
需要“net/http”
url=”http://readwrite.com/2013/12/04/google-compute-engine"
uri=uri(url)
Net::HTTP.get\u响应(uri)
#=> #
Net::HTTP.new(uri.host).request(Net::HTTP::Get.new(url))
#=> #
它只发生在一些URL上。我想不出这个模式。下面是另一个例子:
http://davidduchemin.com/2014/01/towards-mastery-again/
首先,让我们通过使用tcpdump查看它们的实际HTTP请求来比较两者,以便了解可能发生的情况:
tcpdump -vvASs 0 port 80 and host www.readwrite.com
其tcpdump现在实际上与第一个相同:
GET /2013/12/04/google-compute-engine HTTP/1.1
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept: */*
User-Agent: Ruby
Host: readwrite.com
Connection: close
GET/2013/12/04/google计算引擎HTTP/1.1
接受编码:gzip;q=1.0,放气;q=0.6,同一性;q=0.3
接受:*/*
用户代理:Ruby
主持人:readwrite.com
连接:关闭
首先,让我们通过使用tcpdump查看它们的实际HTTP请求来比较两者,以便了解可能发生的情况:
tcpdump -vvASs 0 port 80 and host www.readwrite.com
其tcpdump现在实际上与第一个相同:
GET /2013/12/04/google-compute-engine HTTP/1.1
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept: */*
User-Agent: Ruby
Host: readwrite.com
Connection: close
GET/2013/12/04/google计算引擎HTTP/1.1
接受编码:gzip;q=1.0,放气;q=0.6,同一性;q=0.3
接受:*/*
用户代理:Ruby
主持人:readwrite.com
连接:关闭
我已经试过了,但是它返回了NoMethodError:undefined方法'empty',for#。然而,执行Net::HTTP.new(uri.host.request)(Net::HTTP::Get.new(uri.path))工作得非常好。谢谢另外,感谢您建议使用tcpdump进行调试。它在其他问题上也有很大帮助。我已经尝试过了,但它返回了NoMethodError:undefined method'empty',for#。然而,执行Net::HTTP.new(uri.host.request)(Net::HTTP::Get.new(uri.path))工作得非常好。谢谢另外,感谢您建议使用tcpdump进行调试。它在其他问题上也有很大帮助。 GET /2013/12/04/google-compute-engine HTTP/1.1 Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3 Accept: */* User-Agent: Ruby Host: readwrite.com Connection: close