How to check if System.Net.WebClient.DownloadData is downloading a binary file?

前端 未结 7 731
太阳男子
太阳男子 2020-11-29 00:24

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

7条回答
  •  广开言路
    2020-11-29 01:16

    I'm not sure the cause, but perhaps you hadn't downloaded anything yet. This is the lazy way to get the content type of a remote file/page (I haven't checked if this is efficient on the wire. For all I know, it may download huge chunks of content)

            Stream connection = new MemoryStream(""); // Just a placeholder
            WebClient wc = new WebClient();
            string contentType;
            try
            {
                connection = wc.OpenRead(current.Url);
                contentType = wc.ResponseHeaders["content-type"];
            }
            catch (Exception)
            {
                // 404 or what have you
            }
            finally
            {
                connection.Close();
            }
    

提交回复
热议问题