获取的http:/.../文件大小 [英] Get http:/.../File Size

查看:363
本文介绍了获取的http:/.../文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个http大小:以前我下载/.../文件。该文件可以是网页,图像,或媒体文件。可这与HTTP头做什么?我怎么下载只是文件HTTP头?

I want to get the size of an http:/.../file before I download it. The file can be a webpage, image, or a media file. Can this be done with HTTP headers? How do I download just the file HTTP header?

推荐答案

是的,假设你在跟谁说话支持HTTP服务器/允许这样的:

Yes, assuming the HTTP server you're talking to supports/allows this:

System.Net.WebRequest req = System.Net.HttpWebRequest.Create("http://stackoverflow.com/robots.txt");
req.Method = "HEAD";
using (System.Net.WebResponse resp = req.GetResponse())
{
    int ContentLength;
    if(int.TryParse(resp.Headers.Get("Content-Length"), out ContentLength))
    { 
        //Do something useful with ContentLength here 
    }
}

如果使用HEAD方法是不允许的,或者Content-Length头是不是在服务器响应present,以确定服务器上的内容的大小,唯一的办法就是下载。由于这不是特别可靠,大多数服务器将包括此信息。

If using the HEAD method is not allowed, or the Content-Length header is not present in the server reply, the only way to determine the size of the content on the server is to download it. Since this is not particularly reliable, most servers will include this information.

这篇关于获取的http:/.../文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆