Crystal lang如何从http获取二进制文件

时间:2017-03-03 13:31:31

标签: http binary crystal-lang

在Ruby中:

require 'open-uri'
download = open('http://example.com/download.pdf')
IO.copy_stream(download, '~/my_file.pdf')

如何在Crystal中做同样的事情?

2 个答案:

答案 0 :(得分:7)

我们可以做到以下几点:

require "http/client"

HTTP::Client.get("http://example.org") do |response|
  File.write("example.com.html", response.body_io)
end

这只会将没有任何HTTP标头的响应写入文件。 File.write也非常聪明,不能先将整个文件下载到内存中,而是在读取给定IO的块时写入文件。

答案 1 :(得分:1)

我找到了有用的东西:

require "http/request"
require "file"
res = HTTP::Client.get "https://ya.ru"
fl=File.open("ya.html","wb")
res.to_io(fl)
fl.close