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

前端 未结 7 697
太阳男子
太阳男子 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:08

    Here is a method using TCP, which http is built on top of. It will return when connected or after the timeout (milliseconds), so the value may need to be changed depending on your situation

    var result = false;
    try {
        using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
            var asyncResult = socket.BeginConnect(yourUri.AbsoluteUri, 80, null, null);
            result = asyncResult.AsyncWaitHandle.WaitOne(100, true);
            socket.Close();
        }
    }
    catch { }
    return result;
    

提交回复
热议问题