HTTP下载文件

有些网站的文件,使用程序进行下载,需要注意header。下面是一个例子。

如果不加入下面2行,就无法下载。很多网站是通过Referer防止盗链的。

req.Host = "hldqrcode1.oss-cn-shanghai.aliyuncs.com";

req.Referer = "https://www.ecustpress.cn/";

cs 复制代码
      public static string HttpProc_sessionid(string url, string fn,  ref string err)
        {
            err = "";
            try
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
                req.Timeout = 1000 * 30;
                req.Method = "GET";
                req.AllowAutoRedirect = true;
                req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
                req.Headers.Add("Accept-Encoding", "identity;q=1, *;q=0");
                req.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                req.Headers.Add("Cache-Control", "max-age=0");
                req.KeepAlive = true; 
                req.Host = "hldqrcode1.oss-cn-shanghai.aliyuncs.com";
                // req.Headers.Add("Range", "bytes=0-");
                req.Referer = "https://www.ecustpress.cn/";
                req.Headers.Add("Sec-Fetch-Dest", "document");
                req.Headers.Add("Sec-Fetch-Mode", "navigate");
                req.Headers.Add("Sec-Fetch-Site", "none");
                req.Headers.Add("Sec-Fetch-User", "?1");
                req.Headers.Add("Upgrade-Insecure-Requests", "1");
                req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 Edg/103.0.1264.62";
                req.Headers.Add("sec-ch-ua", @""".Not/A)Brand"";v=""99"", ""Microsoft Edge"";v=""103"", ""Chromium"";v=""103""");
                req.Headers.Add("sec-ch-ua-mobile", "?0");
                req.Headers.Add("sec-ch-ua-platform", @"""Windows""");
                req.ProtocolVersion = HttpVersion.Version11;
                req.CookieContainer = new CookieContainer();


                //Cookie c1 = new Cookie("PHPSESSID",
                //sessionid, "/", req.Host);
                //req.CookieContainer.Add(c1);

                using (WebResponse wr = req.GetResponse())
                {
                    MemoryStream ms = new MemoryStream();
                    wr.GetResponseStream().CopyTo(ms);
                    System.IO.File.WriteAllBytes(fn, ms.ToArray());
                    return fn;
                }
            }
            catch (Exception e)
            {

                err = e.Message;
                return "";
            }

        }

对于HTTPS,还需要响应证书的检查。

public static bool inited = init();

public static bool init()

{

X509Certificate certificate = new X509Certificate();

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

System.Net.ServicePointManager.DefaultConnectionLimit = 512;

return true;

}

private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)

{

return true;

}

cs 复制代码
        public static bool inited = init();
        public static bool init()
        {
            X509Certificate certificate = new X509Certificate();
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

            System.Net.ServicePointManager.DefaultConnectionLimit = 512;
            return true;
        }
        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }
相关推荐
阿乾之铭6 分钟前
HTTP/HTTPS
网络协议·http·https
AI原吾7 分钟前
探索Python的HTTP之旅:揭秘Requests库的神秘面纱
开发语言·python·http·requests
闲人-闲人10 分钟前
HTTP Accept用法介绍
网络·网络协议·http
xiaoxiongip66611 分钟前
HTTP工作原理
网络·网络协议·http·https·ip
_可乐无糖13 分钟前
如何还原 HTTP 请求日志中的 URL 编码参数?详解 %40 到 @
网络·python·https
秋雨雁南飞26 分钟前
C# 命令行运行包
c#·cli
雯0609~1 小时前
C#winform:连接mysql,并将数据展示到页面
开发语言·c#
@Crazy Snail1 小时前
C# 多线程异步--Token
开发语言·c#·token·task
好玩的Matlab(NCEPU)1 小时前
C#+数据库 实现动态权限设置
开发语言·数据库·c#
李李李李李同学2 小时前
等保测评讲解:安全管理中心
网络