简体   繁体   中英

C# UWP HttpWebResponse Chunked

I have problem with read stream Response because I can't read response to end.

This is respon from Server :

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

I try read this response :

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

and this method :

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);

Error Message :

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

Unfortunately it doesn't work, gets only fragments of response. Thank you for your help

I tested your code, for the first method you took, it works fine by me, I just added a Dispose() in it. And for the second method, I think it may be the problem that you didn't get the ContentLength of your var response .

Here is my code, and the commented part of the code is the first method, I took a Button Click event to handle this:

  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();

        }

I've tested it, and it works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM