e.Response.Content in an HTTPResponseHandler returning gzipped content in chrome

4 posts, 1 answers
  1. Steve
    Steve avatar
    35 posts
    Member since:
    Jan 2011

    Posted 12 Jan 2015 Link to this post

    I have an HTTPResponseHandler defined that examines the content returned by the server, to see if the server has sent back the response I am waiting for. The response from the server forthe ContentEncoding parameter of the response is 'gzip'. In IE, I simply look at the string response from Encoding.ASCII.GetString(e.Response.Content) to see if it is the response from the server I am expecting, and all is well. However, when the browser is Chrome, the Response.Content is not being decompressed by the WebAii proxy, and I get a gzipped string back - so I cannot tell if I'm getting what I want back from the server - is there something in the proxy I have to do to tell it to decompress the response?
  2. Answer
    Cody
    Admin
    Cody avatar
    3360 posts

    Posted 15 Jan 2015 Link to this post

    Hi Steve,

    First the WebAii proxy won't automatically decompress the response for you.  It may be that when using IE, depending on the version of IE you're using, the request header is telling the server it cannot accept compressed content.  As a result the server will not return compressed content. Then when you run the test in Chrome, the request header in Chrome tells the server it can accept compressed content and thus you are getting compressed content only in Chrome.

    The easiest solution is to modify the outgoing header and remove the Accept-Encoding header from it. This will force the server to always send uncompressed content and you can easily parse the content.

    The secondary solution is to write code to decompress the content. Something like this: 
    string sBody;
     byte[] content = e.Response.GetUnchunkedContent();
    string sEncoding = e.Response.Headers.GetValues("Content-Encoding");
     if (!String.IsNullOrEmpty(sEncoding))
     {
       if ("gzip".Equals(sEncoding, StringComparison.OrdinalIgnoreCase))
       {
           MemoryStream decompressed = HttpUtilities.DecodeHttpContent("gzip", new MemoryStream(content));
           sBody = Encoding.UTF8.GetString(decompressed.GetBuffer(), 0, (int)decompressed.Length);
       }
       else if ("deflate".Equals(sEncoding, StringComparison.OrdinalIgnoreCase))
       {
           MemoryStream decompressed = HttpUtilities.DecodeHttpContent("deflate", new MemoryStream(content));
           sBody = Encoding.UTF8.GetString(decompressed.GetBuffer(), 0, (int)decompressed.Length);
       }
     }
     else
    {
         sBody = Encoding.UTF8.GetString(content);
     }



    Regards,
    Cody
    Telerik
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
  3. Steve
    Steve avatar
    35 posts
    Member since:
    Jan 2011

    Posted 03 Feb 2015 Link to this post

    Thank you - this is actually very helpful.  I guess I could set up a BeforeRequestListener to go in and strip the Accept-Encoding entry before the request actually goes to the server, couldn't I?
  4. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 05 Feb 2015 Link to this post

    Hi Steve,

    I am glad I could help! Yes you can use BeforeRequestListener to strip the Accept-Encoding entry.

    Regards,
    Cody
    Telerik
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
Back to Top