如何检查System.Net.WebClient.DownloadData是否正在下载二进制文件?

22 c# webclient file download

我试图使用WebClientWinForms应用程序从Web下载文件.但是,我真的只想下载HTML文件.我想要忽略的任何其他类型.

我查了一下WebResponse.ContentType,但它的价值总是如此null.

任何人都知道原因是什么?

Mar*_*ell 56

鉴于您的更新,您可以通过更改GetWebRequest中的.Method来完成此操作:

using System;
using System.Net;
static class Program
{
    static void Main()
    {
        using (MyClient client = new MyClient())
        {
            client.HeadOnly = true;
            string uri = "http://www.google.com";
            byte[] body = client.DownloadData(uri); // note should be 0-length
            string type = client.ResponseHeaders["content-type"];
            client.HeadOnly = false;
            // check 'tis not binary... we'll use text/, but could
            // check for text/html
            if (type.StartsWith(@"text/"))
            {
                string text = client.DownloadString(uri);
                Console.WriteLine(text);
            }
        }
    }

}

class MyClient : WebClient
{
    public bool HeadOnly { get; set; }
    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest req = base.GetWebRequest(address);
        if (HeadOnly && req.Method == "GET")
        {
            req.Method = "HEAD";
        }
        return req;
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以在覆盖GetWebRespons()时检查标头,如果它不是您想要的,可能会抛出异常:

protected override WebResponse GetWebResponse(WebRequest request)
{
    WebResponse resp = base.GetWebResponse(request);
    string type = resp.Headers["content-type"];
    // do something with type
    return resp;
}
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记XHTML:http://www.w3.org/TR/xhtml-media-types/#application-xhtml-xml (2认同)