httpwebrequest 抓取网页数据返回的是application/vnd.ms-excel格式应该怎么处理?

zqaaa1988 2012-06-06 10:29:16
下面是我的代码

HttpWebRequest request = null;
Stream rspStm = null;
//创建Request对象
try
{
request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
request.ContentType = "application/vnd.ms-excel"; //给ContentType赋值
request.CookieContainer = cc; //保存cookie
request.Method = "GET"; //提交的方法
request.Referer = "http://beidou.baidu.com/index.html#/"; //设置标头的值
request.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; .NET4.0C; .NET4.0E)";
request.KeepAlive = true;
request.Accept = "image/jpeg, image/gif, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";

HttpWebResponse response = request.GetResponse() as HttpWebResponse;
cc.Add(response.Cookies);

rspStm = response.GetResponseStream();

StreamReader reader = new StreamReader(rspStm, Encoding.UTF8);

string html = reader.ReadToEnd();

}
catch (Exception)
{

throw;
}
finally
{
rspStm.Close();
request.Abort();
}



但是我提供的网址在Ie输入直接是一个下载页面,抓包抓取出来的数值如下:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=270CAEAD442F9B0690D86A036FA3E402; Path=/report
Content-Disposition: attachment;filename="东方海洋-账户分日报告-20120605-20120605.csv"
Content-Type: application/vnd.ms-excel
Content-Language: zh-CN
Content-Length: 239
Date: Wed, 06 Jun 2012 02:31:13 GMT
Connection: close

日期,展现次数,点击次数,点击率,平均点击价格(元),千次展现成本(元),总费用(元)
2012-06-05,22240,17,0.0764%,0.97,0.74,16.42
合计:,22240,17,0.0764%,0.97,0.74,16.42

这是Stream中的返回值,但是我用上面的程序,一直抓取不到他的内容,求高手指教!不胜感激!
...全文
609 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2012-06-06
  • 打赏
  • 举报
回复
不要使用reader.ReadToEnd();字符串格式的才使用这个


直接存Stream为cvs或者xls
参见
http://dotnet.aspx.cc/file/HttpWebRequest-Download-Http-Url.aspx
孟子E章 2012-06-06
  • 打赏
  • 举报
回复
先看看你的response 得到了正确的内容了没有,你的那个地址是什么?

浏览器执行正确,不代表你的代码也能执行正确
zqaaa1988 2012-06-06
  • 打赏
  • 举报
回复
自己顶一下!
zqaaa1988 2012-06-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
不要使用reader.ReadToEnd();字符串格式的才使用这个


直接存Stream为cvs或者xls
参见
http://dotnet.aspx.cc/file/HttpWebRequest-Download-Http-Url.aspx
[/Quote]
直接存为Stream 那如何把它读取出来呢?我把他读到.xls文件中,里面也是个空值啊。。。

110,533

社区成员

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

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

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