码迷,mamicode.com
首页 > 其他好文 > 详细

由下载地址得到下载包的精确大小

时间:2015-03-17 21:40:41      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTP.html#method-i-head
 
head(path, initheader = nil)click to toggle source

Gets only the header from path on the connected-to host. header is a Hash like { ‘Accept’ => ‘/’, … }.

This method returns a Net::HTTPResponse object.

This method never raises an exception.

response = nil
Net::HTTP.start(‘some.www.server‘, 80) {|http|
  response = http.head(‘/index.html‘)
}
p response[‘content-type‘]

 



于是就有了这样的奇葩方法,欢迎拍砖

  def self.get_the_package_size origin_url
    if origin_url.present?
      if origin_url.index(".com")
        array = origin_url.split(".com").reverse
        url_end = array[0]
        url_begin = array[1].split("//")[1]
        url_begin = "#{url_begin}.com"
      elsif origin_url.index(".net")
        array = origin_url.split(".net").reverse
        url_end = array[0]
        url_begin = array[1].split("//")[1]
        url_begin = "#{url_begin}.net"
      else
        return ""
      end 
      response = nil 
      Net::HTTP.start(url_begin, 80, "10.103.11.54", 81) {|http|
        response = http.head(url_end)
      }   
      return response[‘Content-Length‘]
    end 
  end 

 



其实还可以直接用命令看一下哦
curl -v -XHEAD http://dl.g.youku.com/20150317/1426557582_XXXX-20150312.apk

由下载地址得到下载包的精确大小

标签:

原文地址:http://www.cnblogs.com/iwangzheng/p/4345534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!