kshitijgandhi
kshitijgandhi

Reputation: 1532

Windows Phone using accept-encoding gzip compression in webclient

I need to post data to server, and get compressed data back from it.

I am using windows phone 7 sdk.

I read that it can be done using SharpGIS or Coding4Fun toolkit.

They use WebClient (AFAIK).

can anyone help me?

Here's what I need to do-

  1. Post data(XML) to url

  2. Get compressed data (only GZip supported by server) in the form of xml string/stream

  3. deserialise the xml data received

and the methods should be awaitable.

Upvotes: 0

Views: 460

Answers (3)

Mark Sowul
Mark Sowul

Reputation: 10600

I don't use Windows 8, which means Windows Phone SDK is only on VS 2010, which doesn't support the Microsoft HttpClient.

There's a NuGet package Delay.GZipWebClient written by an MS dev that adds simple support for it. So far it's worked like a charm.

http://blogs.msdn.com/b/delay/archive/2012/04/19/quot-if-i-have-seen-further-it-is-by-standing-on-the-shoulders-of-giants-quot-an-alternate-implementation-of-http-gzip-decompression-for-windows-phone.aspx

Upvotes: 0

Clint Rutkas
Clint Rutkas

Reputation: 280

WebClient and HttpWebRequest for C4F toolkit are supported. HttpClient doesn't exist without http client library currently in WP.

Upvotes: 1

FunksMaName
FunksMaName

Reputation: 2111

When I had to do this in wp7, I

  1. Created a Portable Class Library project within my solution
  2. Nuget the HTTP client library at https://www.nuget.org/packages/Microsoft.Net.Http (Install-Package Microsoft.Net.Http)
  3. Nuget http://www.nuget.org/packages/Microsoft.Bcl.Async/ (Install-Package Microsoft.Bcl.Async ) and add to your PCL and UI solution

With in the portable class library

public class PostData
{
    public async Task<T> TestMe<T>(XElement xml)
    {
        var client = new HttpClient(new HttpClientHandler
        {
            AutomaticDecompression = DecompressionMethods.GZip
                                     | DecompressionMethods.Deflate
        });

        var response = await client.PostAsync("https://requestUri", CreateStringContent(xml));

        var responseString = await response.RequestMessage.Content.ReadAsStringAsync();
        //var responseStream = await response.RequestMessage.Content.ReadAsStreamAsync();
        //var responseByte = await response.RequestMessage.Content.ReadAsByteArrayAsync();

        return JsonConvert.DeserializeObject<T>(responseString);
    }

    private HttpContent CreateStringContent(XElement xml)
    {
        return new StringContent(xml.ToString(), System.Text.Encoding.UTF8, "application/xml");
    }
}

Upvotes: 1

Related Questions