62,041
社区成员
发帖
与我相关
我的任务
分享
private void button6_Click(object sender, EventArgs e)//下载数据文件
{
string downloadAdd = "https://scm.auchan.com.cn/php/scm_items_stat_download.php?sup_no=au&code=all";
string saveFile = "d:\\a.xls";
if (downfile(downloadAdd, saveFile))
{
MessageBox.Show("下载完毕!");
}
else
{
MessageBox.Show("下载过程中出现错误:");
}
}
public bool downfile(string url, string LocalPath)
{
try
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
Uri u = new Uri(url);
HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create(u);
mRequest.Method = "GET";
mRequest.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse wr = (HttpWebResponse)mRequest.GetResponse();
Stream sIn = wr.GetResponseStream();
FileStream fs = new FileStream(LocalPath, FileMode.Create, FileAccess.Write);
long length = wr.ContentLength;
long i = 0;
while (i < length)
{
byte[] buffer = new byte[1024];
i += sIn.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
}
sIn.Close();
wr.Close();
fs.Close();
return true;
}
catch (WebException exp)
{
MessageBox.Show(exp.Message, "error");
return false;
}
}
public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{ // Always accept
return true;
}
命名空间加上
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;