我正在使用net-http-persistent
gem来获取页面。在大多数情况下,它都可以正常工作。但是,最近我注意到,对于以username:password @为前缀的网址,它返回401。 https://username:password@somesite.com。如果我尝试使用其他选项,例如excon
/ curl
,他们会毫无问题地获取此类页面。我看到了Net :: HTTP :: Persistent发出的请求的日志,发现net :: http在连接到服务器时完全丢弃了username:password部分。
任何人都可以帮助我使Net :: HTTP :: Persistent充分利用username:password@
部分。
----------------------编辑--------------------
示例代码:
url = "https://user:pass@example.com/feed"
uri = URI(url)
http = Net::HTTP::Persistent.new
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.request uri
http.shutdown
response.code # yields 401 which it should not as url has username and password.
#Incase of excon, if you do
response = Excon.get(url)
response.status # yields 200 as it is making use of username:password prefix
答案 0 :(得分:2)
基于this issue,请尝试以下代码:
uri = URI("https://example.com/feed")
req = Net::HTTP::Get.new(uri.request_uri)
req.basic_auth 'user', 'pass'
http = Net::HTTP::Persistent.new
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.request uri, req
http.shutdown
puts response.code