C implements the HTTP download method

  • 2020-11-30 08:30:17
  • OfStack

This article illustrated how C# implements HTTP download files. Share to everybody for everybody reference.

The main implementation codes are as follows:

/// <summary>
/// Http The download file
/// </summary>
public static string HttpDownloadFile(string url, string path)
{
    // Set the parameters
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;     // Send the request and get the corresponding response data
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    // until request.GetResponse() The application then starts sending to the target page Post request
    Stream responseStream = response.GetResponseStream();     // Create a local file to write to stream
    Stream stream = new FileStream(path, FileMode.Create);     byte[] bArr = new byte[1024];
    int size = responseStream.Read(bArr, 0, (int)bArr.Length);
    while (size > 0)
    {
        stream.Write(bArr, 0, size);
        size = responseStream.Read(bArr, 0, (int)bArr.Length);
    }
    stream.Close();
    responseStream.Close();
    return path;
}

Hopefully this article has helped you with your C# programming.


Related articles: