Compressed request to Webservice RRS feed

  • Question

  • User-802546231 posted


    this is something I've never tried before so I hope I'm just missing something simple. We have an application which calls an external webservice and we want to compress the requestes. I have tried the following code but it returns an error Cannot close stream until all bytes are written when I reach the end of the GZipStream using statement. Any ideas?

                        HttpWebRequest hwRequest = (HttpWebRequest)HttpWebRequest.Create(strEndPoint);
                        ASCIIEncoding encoding = new ASCIIEncoding();
                        byte[] baToWrite = encoding.GetBytes(rl.strSOAPEnv);
                        hwRequest.Method = "POST";
                        hwRequest.ContentLength = baToWrite.Length;
                        hwRequest.Headers.Add("SOAPAction: \"" + strAction + "\"");
                        hwRequest.ContentType = strContentType;
                        using (Stream stmRequest = hwRequest.GetRequestStream())
                                using (GZipStream zipStream = new GZipStream(stmRequest, CompressionMode.Compress))
                                    zipStream.Write(baToWrite, 0, baToWrite.Length);
                        using (HttpWebResponse hwResponse = (HttpWebResponse)hwRequest.GetResponse())
    ... parse responseetc

    Friday, October 4, 2013 6:35 AM


  • User-1662538993 posted

    Please try to add this-

    Response.AddHeader("Content-Length", zipSize);

    zipStream.Write(baToWrite, 0, baToWrite.Length);

    What you are trying to do is you are trying to read until the baToWrite.Lenght. You should read until the length.


    zipStream.Write(baToWrite, 0, baToWrite.ReadToEnd);

    // Could not remember exact for readtoend but try something like that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 4, 2013 3:49 PM