相关疑难解决方法(0)

解压缩来自WebClient的gzip响应

有没有一种快速方法来解压缩使用WebClient.DownloadString()方法下载的gzip响应?您对如何使用WebClient处理gzip响应有任何建议吗?

.net c# webclient

38
推荐指数
1
解决办法
2万
查看次数

如何在C#中读取两次Http响应流?

我试图通过以下两次读取Http响应流:

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
RssReader reader = new RssReader(stream);
do
{
  element = reader.Read();
  if (element is RssChannel)
  {
    feed.Channels.Add((RssChannel)element);
  }
} while (element != null);

StreamReader sr = new StreamReader(stream);
feed._FeedRawData = sr.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)

但是,当StreamReader代码执行时,没有返回数据,因为流现在已经到达终点.我尝试通过stream.Position = 0重置流,但这会引发异常(我认为因为流不能手动更改其位置).

基本上,我想解析XML的流并且可以访问原始数据(以字符串格式).

有任何想法吗?

c# stream

36
推荐指数
1
解决办法
3万
查看次数

如果返回内容是Transfer-Encoding:chunked,如何从HttpWebResponse获取完整内容?

我正在编写一个从其他网站下载html页面的程序.我发现一个问题,对于某些特定的网站,我无法获得完整的HTML代码.我只能获得部分内容.有这个问题的服务器在"Transfer-Encoding:chunked"中发送数据,恐怕这就是问题的原因.

这是服务器返回的头信息:

Transfer-Encoding: chunked
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Content-Type: text/html; charset=UTF-8
Date: Sun, 11 Sep 2011 09:46:23 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Server: nginx/1.0.6
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
HttpWebResponse response;
CookieContainer cookie = new CookieContainer();
request.CookieContainer = cookie;
request.AllowAutoRedirect = true;
request.KeepAlive = true;
request.UserAgent =
    @"Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2 FirePHP/0.6";
request.Accept = @"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
string html = string.Empty;
response = request.GetResponse() as HttpWebResponse;

using (StreamReader …
Run Code Online (Sandbox Code Playgroud)

.net c# web

14
推荐指数
2
解决办法
2万
查看次数

C#使用WebClient下载分块编码内容

我写了一个客户端应用程序,假设从Web服务器下载文件,非常简单:

using (WebClient webClient = new WebClient())
{
    webClient.DownloadFile("http://localhost/audiotest/audio.wav", 
                           @"C:\audio.wav");
}
Run Code Online (Sandbox Code Playgroud)

该网站(音频文件位于:http://localhost/audiotest/audio.wav)具有标题Transfer-Encoding:chunked

当我运行该程序时,我收到以下错误:

服务器提交了协议违规.Section = ResponseBody Detail =响应块格式无效

当服务器包含Transfer-Encoding:chunked header时,如何下载文件?

c# chunked-encoding

7
推荐指数
1
解决办法
5065
查看次数

标签 统计

c# ×4

.net ×2

chunked-encoding ×1

stream ×1

web ×1

webclient ×1