c#如何通过实现下载链接所生成的excel文件

爬爬爬往死爬 2017-12-01 10:26:03
如题.
下载:http://pub.alimama.com/report/getTbkPaymentDetails.json?spm=0.0.0.0.Oq98lr&queryType=1&payStatus=3&DownloadID=DOWNLOAD_REPORT_INCOME_NEW&startTime=2014-12-05&endTime=2014-12-11

比如这个是阿里妈妈的数据报表。是需要执行url后才会获得下载文件。
直接运行ie是可以获得下载文件。并弹出保存对话框。
那么如何通过c#实现下载这样链接所生成的文件

百度的方法试了,但是下载下来的是页面信息,并不是生成的xls文件

代码如下:
/// <summary>
/// 下载需要得xls
/// </summary>

private string DownloadFile(string starttime, string endtime, int p)
{
string fileName = "";
try
{
object[] args = new object[] { starttime, endtime, p, Http.Token };
string url = string.Format("http://pub.alimama.com/report/getTbkPaymentDetails.json?queryType=1&payStatus=&DownloadID=DOWNLOAD_REPORT_INCOME_NEW&startTime={0}&endTime={1}&toPage={2}&perPageSize=100&_tb_token_={3}&_input_charset=utf-8", args);
fileName = CreateFileName(url);
if (!Directory.Exists(Application.StartupPath + "/"))
{
Directory.CreateDirectory(Application.StartupPath + "/");
}
WebClient client = new WebClient();
client.DownloadFile(url, Application.StartupPath + "/" + fileName);
}
catch (Exception e)
{
Console.WriteLine(e);
return "";
}
if (!String.IsNullOrEmpty(fileName))
{
Console.WriteLine("文件下载成功,文件名称:" + fileName);
}
else
{
Console.WriteLine("文件下载失败");
}
Console.ReadLine();
return fileName;
}

/// <summary>
/// 创建文件名称
/// </summary>
public static string CreateFileName(string url)
{
string fileName = "";
string startTime = url.Substring(url.IndexOf("startTime=") + 10, 10);
string endTime = url.Substring(url.IndexOf("endTime=") + 8, 10);
fileName = "TaokeDetail" + startTime + "-" + endTime + ".xls";
Console.WriteLine(fileName);
return fileName;
}
...全文
724 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZY_zhu 2018-06-29
  • 打赏
  • 举报
回复
防盗链关键代码:
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(httpUrl);
webrequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/QVOD, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
webrequest.Headers.Add("Accept-Encoding", "gzip, deflate");
webrequest.Headers.Add("Accept-Language", "zh-cn");
webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; MS-RTC LM 8; Alexa Toolbar)";
webrequest.Headers.Add("UA-CPU", "x86");
webrequest.KeepAlive = true;
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
事理 2017-12-01
  • 打赏
  • 举报
回复
WebClient client=new WebClient(); client.DownLoad(url,"本地路径");
wushang923 2017-12-01
  • 打赏
  • 举报
回复
WebClient client = new WebClient(); client.DownloadFile("http://xxxxxxxxxx", "D:\\123.xlsx"); var req = (HttpWebRequest)WebRequest.Create("http://xxxxxxxxxx"); req.Method = "GET"; req.ContentType = "application/vnd.ms-excel";//EXCEL req.Timeout = 30000; var rsp = (HttpWebResponse)req.GetResponse(); var requestStream = rsp.GetResponseStream(); if (requestStream != null) { using (var fileStream = new FileStream("D:\\123.xlsx", FileMode.Create, FileAccess.Write)) { requestStream.CopyTo(fileStream); } }
爬爬爬往死爬 2017-12-01
  • 打赏
  • 举报
回复
引用 1 楼 slyzly 的回复:
WebClient client=new WebClient(); client.DownLoad(url,"本地路径");
WebClient client=new WebClient(); client.DownLoad(url,"本地路径"); 哥们,没有DownLoad这个方法呀,DownLoadFile这个方法下载出来的xls文件内容是错误的。
爬爬爬往死爬 2017-12-01
  • 打赏
  • 举报
回复
WebClient client=new WebClient(); client.DownLoad(url,"本地路径"); 哥们,没有DownLoad这个方法呀,DownLoadFile这个方法下载出来的xls文件内容是错误的。

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧