有些网站的文件,使用程序进行下载,需要注意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;
}