如何检查 System.Net.WebClient.DownloadData 是否正在下载二进制文件? [英] How to check if System.Net.WebClient.DownloadData is downloading a binary file?

查看:16
本文介绍了如何检查 System.Net.WebClient.DownloadData 是否正在下载二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WebClient 使用 WinForms 应用程序从 Web 下载文件.但是,我真的只想下载 HTML 文件.我想忽略的任何其他类型.

I am trying to use WebClient to download a file from web using a WinForms application. However, I really only want to download HTML file. Any other type I will want to ignore.

我检查了 WebResponse.ContentType,但它的值总是 null.

I checked the WebResponse.ContentType, but its value is always null.

有人知道可能是什么原因吗?

Anyone have any idea what could be the cause?

推荐答案

根据您的更新,您可以通过更改 GetWebRequest 中的 .Method 来实现:

Given your update, you can do this by changing the .Method in GetWebRequest:

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;
    }
}

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

Alternatively, you can check the header when overriding GetWebRespons(), perhaps throwing an exception if it isn't what you wanted:

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

这篇关于如何检查 System.Net.WebClient.DownloadData 是否正在下载二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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