【问题标题】:C# UWP HttpWebResponse ChunkedC# UWP HttpWebResponse 分块
【发布时间】:2015-11-18 12:32:55
【问题描述】:

我在读取流响应时遇到问题,因为我无法读取结束响应。

这是来自服务器的响应:

 Server : "Apache-Coyote/1.1"
 Transfer-Encoding : "chunked"
 Content-Type: "multipart/mixed; boundary=F34D3847AEDEB14FF5967BF7426EECF6"

我尝试阅读此回复:

var response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
using(var read = new StreamReader(response.GetResponseStream())
 {
   var result = await read.ReadToEndAsync();
 }

还有这个方法:

StringBuilder sb = new StringBuilder();
Byte[] buf = new byte[8192];
Stream resStream = response.GetResponseStream();
string tmpString = null;
int count = 0;
do
{
count = resStream.Read(buf, 0, buf.Length);
if(count != 0)
{
      tmpString = Encoding.ASCII.GetString(buf, 0, count);
      sb.Append(tmpString);
}
}while (count > 0);

错误信息:

Exception from HRESULT: 0x80072EE4
I/O error occurred.
Exception thrown: 'System.IO.IOException' in mscorlib.ni.dll
Exception thrown: 'System.IO.IOException' in mscorlib.ni.dll

不幸的是,它不起作用,只得到响应的片段。谢谢你的帮助

【问题讨论】:

    标签: c# windows uwp httpwebresponse chunked


    【解决方案1】:

    我测试了您的代码,对于您采用的第一种方法,我运行良好,我只是在其中添加了一个 Dispose()。而对于第二种方法,我认为可能是你没有得到你的 var responseContentLength 的问题。

    这是我的代码,代码中注释的部分是第一种方法,我拿了一个Button Click事件来处理这个:

      public async void HTTP(object sender, RoutedEventArgs e)
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("URL");
                HttpWebResponse response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
                Stream resStream = response.GetResponseStream();            
    
                StringBuilder sb = new StringBuilder();
    
                StreamReader read = new StreamReader(resStream);
                string tmpString = null;
                int count = (int)response.ContentLength;
                int offset = 0;
                Byte[] buf = new byte[count];
                do
                {
                    int n = resStream.Read(buf, offset, count);
                    if (n == 0) break;
                    count -= n;
                    offset += n;
                    tmpString = Encoding.ASCII.GetString(buf, 0, buf.Length);
                    sb.Append(tmpString);
                } while (count > 0);
    
                text.Text = tmpString;
    
    
                read.Dispose();
    
                //using (StreamReader read = new StreamReader(resStream))
                //{
                //    var result = await read.ReadToEndAsync();
                //    text.Text = result;
                //    read.Dispose();
                //}
    
                response.Dispose();
    
            }
    

    我已经测试过了,效果很好。

    【讨论】:

    • 感谢您的回复。我测试了这种方法,但在服务器响应中:Content-Length 为 -1,我在 WireShark 和 MultiPart 中测试了服务器,并且 ChunkedSum 很好。我不知道...
    • 因为某些Server响应使用gzipdeflate算法来加速传输速率,所以header中不会有ContentLength。你可以google一下,然后你就可以纠正这个错误了。
    猜你喜欢
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多