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;
        }
相关推荐
音徽编程2 小时前
Rust异步运行时框架tokio保姆级教程
开发语言·网络·rust
m0_656974743 小时前
C#中的集合类及其使用
开发语言·c#
九鼎科技-Leo3 小时前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
幺零九零零3 小时前
【C++】socket套接字编程
linux·服务器·网络·c++
23zhgjx-NanKon4 小时前
华为eNSP:QinQ
网络·安全·华为
23zhgjx-NanKon4 小时前
华为eNSP:mux-vlan
网络·安全·华为
点点滴滴的记录4 小时前
RPC核心实现原理
网络·网络协议·rpc
Lionhacker4 小时前
网络工程师这个行业可以一直干到退休吗?
网络·数据库·网络安全·黑客·黑客技术
程思扬5 小时前
为什么Uptime+Kuma本地部署与远程使用是网站监控新选择?
linux·服务器·网络·经验分享·后端·网络协议·1024程序员节
ZachOn1y5 小时前
计算机网络:运输层 —— 运输层概述
网络·tcp/ip·计算机网络·运输层