获取前缀为 username:password@ 的网址

Fetch urls prefixed with username:password@

我正在使用 net-http-persistent gem 来获取页面。对于大多数情况,它工作得很好。但是,最近我注意到它 returns 401 对于前缀为 username:password@ 的网址,例如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

基于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