c# winform程序怎么样通过百度云盘下载文件

.Net技术 码拜 9年前 (2016-06-09) 2232次浏览
本人现在需要做一个项目(网络学习平台),项目中会用到一些资源文件(学习课件),本人想把这些资源放在百度云盘上,在程序中通过百度云盘的分享地址实现自动下载。请高手职教。
解决方案

10

可以调用百度云的Api,看这里

20

错误码是什么,可以对照这个

引用:

本人只是需要通过百度云盘分享出来的网址来下载文件,然后总是报403错误。是必须要申请API Key(client_id)与Secret Key(client_secret)和access Token吗?

  public class BaiDuYunDownLoad
    {
       public static void DownLoad(string url, string saveFilePath)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
                request.Referer = url;
                request.Method = "GET";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)";
                request.ContentType = "application/octet-stream";
                request.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                request.Timeout = 20*1000;
                request.AllowAutoRedirect = true;
                //request.ClientCertificates.Add(new X509Certificate2(cert_path,cert_password));
                HttpWebResponse response = (HttpWebResponse) request.GetResponse();
  
                Stream responseStream = response.GetResponseStream();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    FileStream fileStream = new FileStream(saveFilePath, FileMode.Create, FileAccess.ReadWrite);
                    if (responseStream != null)
                    {
                        responseStream.Flush();
                        StreamReader sr = new StreamReader(responseStream);
                        string buffer = sr.ReadToEnd();
                        byte[] bt = new UTF8Encoding(true).GetBytes(buffer);
                        fileStream.Write(bt, 0, bt.Length);
                    }
                    fileStream.Close();
                }
                else
                {
                    throw new Exception("服务器返回状态失败,StatusCode:" + response.StatusCode);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw new Exception("无法连接下载地址");
            }
        }
    }

10

引用:

本人只是需要通过百度云盘分享出来的网址来下载文件,然后总是报403错误。是必须要申请API Key(client_id)与Secret Key(client_secret)和access Token吗?

  public class BaiDuYunDownLoad
    {
       public static void DownLoad(string url, string saveFilePath)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
                request.Referer = url;
                request.Method = "GET";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)";
                request.ContentType = "application/octet-stream";
                request.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                request.Timeout = 20*1000;
                request.AllowAutoRedirect = true;
                //request.ClientCertificates.Add(new X509Certificate2(cert_path,cert_password));
                HttpWebResponse response = (HttpWebResponse) request.GetResponse();
  
                Stream responseStream = response.GetResponseStream();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    FileStream fileStream = new FileStream(saveFilePath, FileMode.Create, FileAccess.ReadWrite);
                    if (responseStream != null)
                    {
                        responseStream.Flush();
                        StreamReader sr = new StreamReader(responseStream);
                        string buffer = sr.ReadToEnd();
                        byte[] bt = new UTF8Encoding(true).GetBytes(buffer);
                        fileStream.Write(bt, 0, bt.Length);
                    }
                    fileStream.Close();
                }
                else
                {
                    throw new Exception("服务器返回状态失败,StatusCode:" + response.StatusCode);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw new Exception("无法连接下载地址");
            }
        }
    }

你需要在百度上申请一下开发者账号,然后按照一楼的操作来做


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c# winform程序怎么样通过百度云盘下载文件
喜欢 (0)
[1034331897@qq.com]
分享 (0)