Coder Social home page Coder Social logo

sharpgis.gzipwebclient's Introduction

###GZip Compression for Windows Phone WebClient

UPDATE: THIS PROJECT HAS BEEN DISCONTINUED. PLEASE USE THE OFFICIAL MICROSOFT PROVIDED HTTP CLIENT LIBRARY: https://www.nuget.org/packages/Microsoft.Net.Http

Now that Microsoft finally provides GZip compression for Windows Phone, I encourage you to use their official nuget package instead.

======================

(c) Copyright Morten Nielsen.

This source is subject to the Microsoft Public License (Ms-PL). Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. All other rights reserved.

Available for direct install from NuGet: https://nuget.org/packages/SharpGIS.GZipWebClient

To use this code, explicitly use the SharpGIS.GZipWebClient class where you would normally use a WebClient instance. Ex:

   WebClient client = new SharpGIS.WebClient(); //Yes the type is still WebClient since that's what it inherits from :-)
   //continue with this WebClient as you used to

Or you can automatically opt in for all existing WebClients by adding the following code during application start up:

   WebRequest.RegisterPrefix("http://", SharpGIS.WebRequestCreator.GZip);
   WebRequest.RegisterPrefix("https://", SharpGIS.WebRequestCreator.GZip);

This will change ALL web requests both those that uses WebClient and those that uses the lower-level HttpWebRequest, as well as any 3rd party library to start using compressed requests. Please test thoroughly - I've seen some issues with a few 3rd party libraries that doesn't work well with a different HttpWebRequest object, but for the most part this just works.

Blogpost on this utility available here: http://www.sharpgis.net/post/2011/08/28/GZIP-Compressed-Web-Requests-in-WP7-Take-2.aspx

sharpgis.gzipwebclient's People

Contributors

dotmorten avatar richardszalay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sharpgis.gzipwebclient's Issues

NuGet package not up to date

If I use the published 1.4.0 nuget package, and set a UserAgent, that user agent is discarded and "Test" is sent instead. If I build from source, it works fine. Could you please check if the nuget package has the right code?

Error while closing the stream reader when is over GZipStream

I am using a stream reader over GZipStream. And trying to read line.

                        var streamReadr = new StreamReader(new GZipStream(streamResponse));

                        string ch;
                        var responseStr = "";
                        while ((ch=streamReadr.ReadLine()) !=null)
                        {
                            responseStr += ch;
                        }
                        streamReadr.Close();
                        streamResponse.Close();
                        webResponse.Close();

I am getting an error while closing StreamReader instance, very very rarely.

"Protocol error. AvailableBytesIn=5, expected 8"
in file : ZLibStream.cs on the line
In function:"finish()"

values were :
InpuBuffer size 8192 bytes
NextIn value is 8187
trailer is of 8 bytes

I didnot understand why this case has come.
Does this mean CRC check has failed.
But it is happening for only a certain parameters to the same api. From the earlier code I am able retrieve the complete responseStr but getting an error in finish.
It is working normally for other platforms with different libraries.
I am looking into this but want to report to you.
I am not sure if this is an issue or not.

WebExceptions are not properly handled

If WebClient returns an error, NPE will be thrown, because the WebException is not properly handled.

The GZipWebClient::GetWebResponse method should look like this:
protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
{
WebResponse response;
try
{
response = base.GetWebResponse(request, result);
}
catch (WebException we)
{
response = we.Response;
}
catch
{
response = null;
}

return response != null
            ? (response.Headers[HttpRequestHeader.ContentEncoding] == "gzip"
                    ? new GZipWebResponse(response)
                    : response)
            : null;

}

Encoding request streams

Hey there, this is a great help for downloading large amounts of data; I've looked through the source and I don't see any way of encoding large amounts of data to, for example, POST to a webservice. Is there any such functionality available? And if there isn't, is there a simple way to work this kind of functionality into your framework?

GZipWebClient throws NotImplementedException when used in WP7 background agent

Apparently this check fails

if (_deflatedStream.CanSeek)
    _deflatedStream.Seek(6, SeekOrigin.Current);

Even though the stream claims it supports seek, it actually does not, resulting in NotImplementedException being thrown.

I suspect it has something to do with the different implementation of streams in background agents, since the streams in the main assembly work as expected.

As a workaround, I removed CanSeek check and always do 6 byte reads and it seems to work even for background agents.

Make GZipWebResponse inherit from HttpWebResponse instead of WebResponse

To support stuff like getting the HttpStatusCode and more. I propose the following changes to the class (I was unable to fork and change it myself).

    internal class GZipWebResponse : HttpWebResponse
    {
        WebResponse response;
        internal GZipWebResponse(WebResponse resp)
        {
            response = resp;
        }

        public override System.IO.Stream GetResponseStream()
        {
            return new SharpGIS.ZLib.GZipStream(response.GetResponseStream());
        }
        public override void Close()
        {
            response.Close();
        }
        public override long ContentLength
        {
            get { return response.ContentLength; }
        }
        public override string ContentType
        {
            get { return response.ContentType; }
        }
        public override WebHeaderCollection Headers
        {
            get { return response.Headers; }
        }
        public override Uri ResponseUri
        {
            get { return response.ResponseUri; }
        }
        public override bool SupportsHeaders
        {
            get { return response.SupportsHeaders; }
        }

        public override CookieCollection Cookies
        {
            get
            {
                HttpWebResponse httpResponse = response as HttpWebResponse;
                if (httpResponse != null)
                    return httpResponse.Cookies;
                else
                    throw new NotImplementedException();
            }
        }

        public override HttpStatusCode StatusCode
        {
            get
            {
                HttpWebResponse httpResponse = response as HttpWebResponse;
                if (httpResponse != null)
                    return httpResponse.StatusCode;
                else
                    throw new NotImplementedException();
            }
        }

        public override string Method
        {
            get
            {
                HttpWebResponse httpResponse = response as HttpWebResponse;
                if (httpResponse != null)
                    return httpResponse.Method;
                else
                    throw new NotImplementedException();
            }
        }

        public override string StatusDescription
        {
            get
            {
                HttpWebResponse httpResponse = response as HttpWebResponse;
                if (httpResponse != null)
                    return httpResponse.StatusDescription;
                else
                    throw new NotImplementedException();
            }
        }
    }

Cannot get HttpWebResponse.

On the client when using GZipWebClient and throwing an error on the server like so:
return new HttpResponseMessage(HttpStatusCode.NotAcceptable);

the error message on the client always fails with the below on this line:
WebException we = (WebException)e.Error;
HttpWebResponse response = (System.Net.HttpWebResponse)we.Response;

Message:

InnerException = {System.NullReferenceException: Object reference not set to an instance of an object.
at System.Net.WebClient.DownloadBitsState.SetResponse(WebResponse response)
at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)}

Message = "An exception occurred during a WebClient request. "

This work fine when using standard WebClient, is there some other way I to successfully get the HttpWebResponse ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.