Transfer-Encoding: chunked in Windows Phone

蓝咒 提交于 2019-12-01 21:45:06

问题


I have a server response with Transfer-Encoding: chunked

HTTP/1.1 200 OK
Server: nginx/1.2.1
Date: Mon, 18 Feb 2013 08:22:49 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding

c7
{<some json data>}
0

See that c7 chunk size before json data.

How can I read a raw response stream without chunks in Windows Phone using HttpWebResponse?


Hint: to make sever disable chunked output, I just have to specify HTTP/1.0 protocol version. But I don't know how to do that as there is no ProtocolVersion property in HttpWebRequest class in Windows Phone or Silverlight


回答1:


HttpClient is able to parse chunked output automatically http://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.110).aspx

HttpClient is an overall cool thing with PostAsync and GetAsinc and tons of other goodness. I never-ever use HttpWebRequest again.

HttpClient is readily available in .NET Framework 4.5, Windows 8, or Windows Phone 8.1

Use NuGet package http://www.nuget.org/packages/Microsoft.Net.Http if you need HttpClient in - .NET Framework 4 - Windows Phone Silverlight 7.5 - Silverlight 4 - Portable Class Libraries




回答2:


You can read chunked response in the following way:

public static byte[] ReadChunkedResponse(this WebResponse response)
    {
        byte[] buffer;

        using (var stream = response.GetResponseStream())
        {
            using (var streamReader = new StreamReader(stream, Encoding.UTF8))
            {
                var content = new StringBuilder();
                while (!streamReader.EndOfStream)
                {
                    content.Append((char)streamReader.Read());
                }

                buffer = Encoding.UTF8.GetBytes(content.ToString());
            }
        }

        return buffer;
    }


来源:https://stackoverflow.com/questions/14932183/transfer-encoding-chunked-in-windows-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!