我正在尝试使用 ruby 下载此文件:
https://evs.nci.nih.gov/ftp1/CDISC/ReadMe.txt
这是一个示例程序,显示了问题(评论显示了我得到的内容):
require 'uri'
require 'net/http'
require 'pp'
url = 'https://evs.nci.nih.gov/ftp1/CDISC/ReadMe.txt'
uri = URI.parse(url)
begin
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.start
p http.get(uri.path)
http.finish
rescue => ex
puts "error: #{ex.message} (#{ex.class})"
# error: Failed to open TCP connection to evs.nci.nih.gov:443 (Blocking operation timed out!) (IO::TimeoutError)
end
我试图了解问题出在哪里,发现问题是这样的:
begin
p TCPSocket.new("evs.nci.nih.gov", 443, connect_timeout: 90)
rescue => ex
puts "error: #{ex.message} (#{ex.class})"
# error: Blocking operation timed out! (IO::TimeoutError)
end
我试图获取更多这样的信息:
pp Addrinfo.getaddrinfo("evs.nci.nih.gov", 443)
# [#<Addrinfo: [2600:1f18:2102:3600:b4de:3448:7a8c:7ecd]:443 (evs.nci.nih.gov)>,
# #<Addrinfo: [2600:1f18:2102:3601:4147:d992:65b4:4ac1]:443 (evs.nci.nih.gov)>,
# #<Addrinfo: 52.1.83.88:443 (evs.nci.nih.gov)>,
# #<Addrinfo: 3.224.99.192:443 (evs.nci.nih.gov)>]
现在可以了:
p TCPSocket.new("3.224.99.192", 443, connect_timeout: 90)
# #<TCPSocket:fd 3, AF_INET, 192.168.1.25, 2433>
好吧,那我们试试吧:
url = 'https://3.224.99.192/ftp1/CDISC/ReadMe.txt'
uri = URI.parse(url)
begin
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.start
p http.get(uri.path)
http.finish
rescue => ex
puts "error: #{ex.message} (#{ex.class})"
# error: hostname "3.224.99.192" does not match the server certificate (OpenSSL::SSL::SSLError)
end
是的,我能弄清楚。
我尝试过卷曲:
curl -o "ReadMe.txt" --trace-ascii "curl.log" "https://evs.nci.nih.gov/ftp1/CDISC/ReadMe.txt"
它就像一个魅力。 这是日志:
== Info: Trying [2600:1f18:2102:3601:4147:d992:65b4:4ac1]:443...
== Info: Trying 3.224.99.192:443...
== Info: Connected to evs.nci.nih.gov (3.224.99.192) port 443
== Info: schannel: disabled automatic use of client certificate
== Info: ALPN: curl offers http/1.1
== Info: ALPN: server accepted http/1.1
== Info: using HTTP/1.1
=> Send header, 99 bytes (0x63)
0000: GET /ftp1/CDISC/ReadMe.txt HTTP/1.1
0025: Host: evs.nci.nih.gov
003c: User-Agent: curl/8.4.0
0054: Accept: */*
0061:
<= Recv header, 17 bytes (0x11)
0000: HTTP/1.1 200 OK
<= Recv header, 37 bytes (0x25)
0000: Date: Sat, 20 Apr 2024 16:27:02 GMT
<= Recv header, 26 bytes (0x1a)
0000: Content-Type: text/plain
<= Recv header, 22 bytes (0x16)
0000: Content-Length: 2157
<= Recv header, 24 bytes (0x18)
0000: Connection: keep-alive
<= Recv header, 30 bytes (0x1e)
0000: Server: Apache/2.4.54 (Unix)
<= Recv header, 46 bytes (0x2e)
0000: Strict-Transport-Security: max-age=31536000;
<= Recv header, 46 bytes (0x2e)
0000: Last-Modified: Fri, 29 Mar 2024 04:31:09 GMT
<= Recv header, 27 bytes (0x1b)
0000: ETag: "86d-614c51be679a1"
<= Recv header, 22 bytes (0x16)
0000: Accept-Ranges: bytes
<= Recv header, 2 bytes (0x2)
0000:
<= Recv data, 2157 bytes (0x86d)
0000: March 29, 2024.
(...)
086b: SC
== Info: Connection #0 to host evs.nci.nih.gov left intact
知道 ruby 出了什么问题吗?
我运行了你的代码片段,我得到了
#<Net::HTTPOK 200 OK readbody=true>
,所以我相信问题不在于程序本身。
潜在原因: