手动下载的EXE可以 但是用HttpRequest下载的文件就打不开 提示不是有效的WIN32..
着急中 贴出代码 希望达人指教
public static bool downfile(string url, string LocalPath, CookieContainer CookieCheck)
{
try
{
Uri u = new Uri(url);
HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create(u);
mRequest.CookieContainer = CookieCheck;
mRequest.Method = "GET";
//mRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; ShopperReports 3.0.485.0; SRS_IT_E8790270BC76595134AC91; .NET CLR 1.1.4322)";
//mRequest.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
mRequest.ContentType = "application/octet-stream";
HttpWebResponse wr = (HttpWebResponse)mRequest.GetResponse(); wr.Cookies = CookieCheck.GetCookies(mRequest.RequestUri); Stream sIn = wr.GetResponseStream();
FileStream fs = new FileStream("FLVPro.exe", FileMode.Create, FileAccess.Write); long length = wr.ContentLength;
long i = 0;
decimal j = 0; while (i < length)
{
byte[] buffer = new byte[1024];
i += sIn.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length); if ((i % 1024) == 0)
{
j = Math.Round(Convert.ToDecimal((Convert.ToDouble(i) / Convert.ToDouble(length)) * 100), 4);
// statusBar1.Text = "当前下载文件大小:" + length.ToString() + "字节 当前下载大小:" + i + "字节 下载进度" + j.ToString() + "%";
Console.WriteLine("当前下载文件大小:" + length.ToString() + "字节 当前下载大小:" + i + "字节 下载进度" + j.ToString() + "%"); }
else
{
//statusBar1.Text = "当前下载文件大小:" + length.ToString() + "字节 当前下载大小:" + i + "字节";
Console.WriteLine("当前下载文件大小:" + length.ToString() + "字节 当前下载大小:" + i + "字节");
} } sIn.Close();
wr.Close();
fs.Close();
return true;
}
catch {
return false; }
这一个链接是需要COOKIES的 不知道迅雷是怎样获取而去下载的 用迅雷又可以下载 奇怪 是我哪里写得不对吗 希望达人指教!
着急中 贴出代码 希望达人指教
public static bool downfile(string url, string LocalPath, CookieContainer CookieCheck)
{
try
{
Uri u = new Uri(url);
HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create(u);
mRequest.CookieContainer = CookieCheck;
mRequest.Method = "GET";
//mRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; ShopperReports 3.0.485.0; SRS_IT_E8790270BC76595134AC91; .NET CLR 1.1.4322)";
//mRequest.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
mRequest.ContentType = "application/octet-stream";
HttpWebResponse wr = (HttpWebResponse)mRequest.GetResponse(); wr.Cookies = CookieCheck.GetCookies(mRequest.RequestUri); Stream sIn = wr.GetResponseStream();
FileStream fs = new FileStream("FLVPro.exe", FileMode.Create, FileAccess.Write); long length = wr.ContentLength;
long i = 0;
decimal j = 0; while (i < length)
{
byte[] buffer = new byte[1024];
i += sIn.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length); if ((i % 1024) == 0)
{
j = Math.Round(Convert.ToDecimal((Convert.ToDouble(i) / Convert.ToDouble(length)) * 100), 4);
// statusBar1.Text = "当前下载文件大小:" + length.ToString() + "字节 当前下载大小:" + i + "字节 下载进度" + j.ToString() + "%";
Console.WriteLine("当前下载文件大小:" + length.ToString() + "字节 当前下载大小:" + i + "字节 下载进度" + j.ToString() + "%"); }
else
{
//statusBar1.Text = "当前下载文件大小:" + length.ToString() + "字节 当前下载大小:" + i + "字节";
Console.WriteLine("当前下载文件大小:" + length.ToString() + "字节 当前下载大小:" + i + "字节");
} } sIn.Close();
wr.Close();
fs.Close();
return true;
}
catch {
return false; }
这一个链接是需要COOKIES的 不知道迅雷是怎样获取而去下载的 用迅雷又可以下载 奇怪 是我哪里写得不对吗 希望达人指教!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货