C# UWP HttpWebResponse Chunked

1

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

c#
windows
uwp
httpwebresponse
chunked
asked on Stack Overflow Nov 18, 2015 by lshadown

1 Answer

0

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.

answered on Stack Overflow Nov 19, 2015 by panda • edited Nov 19, 2015 by panda

User contributions licensed under CC BY-SA 3.0