C# Http带token下载远程文件

/// <summary>
/// Http带token下载远程文件,
/// </summary>
public static void HttpDownloadFile(string url, string OutputPath, string fileName, string token)
{
    // 设置参数
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.Headers.Add("X-Auth-token", token);
    //发送请求并获取相应回应数据
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    //直到request.GetResponse()程序才开始向目标网页发送Post请求
    Stream responseStream = response.GetResponseStream();

    if (!Directory.Exists(OutputPath))
    {
        // 目录不存在,创建目录
        Directory.CreateDirectory(OutputPath);
    }

    //创建本地文件写入流
    Stream stream = new FileStream(fileName, 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();
}

 

来源地址:C# Http带token下载远程文件

转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:www.88531.cn资享网,谢谢!^^

© 版权声明
THE END
喜欢就支持一下吧
点赞45 分享