HttpWebRequest compression using gzip

I am developing a console application .NET 4.0to work as a client SOAP Web Servicethat sends data (POST) through third-party ones. I have no control over the server side web service. A third party provided WSDL'sfor use, and I was able to import them and use them with reasonable success. However, there is a requirement to compress the request message using gzip, and for the rest of my life I could not figure out how to do this using proxy services.

This section here on SO made me believe that without control over the client and server code, the request cannot be compressed. As a result of this output, I wrote code in my application to manually create an object SOAP XMLin XDocument; then filled in the values ​​from the WSDLproxy class class objects that I previously encoded for my client application.

The first requirement for this client is to send a message compressed with gzip. After some research, I saw the answers as simple as adding them HttpRequestHeader.AcceptEncoding, "gzip, deflate"to the request header. Unfortunately this does not work.

Currently, the certificate I receive is not a real certificate. I am trying to make the code as strong as I can before deploying it in a test environment to do the actual testing of the service.

  • Is there a way to compress the request through a proxy call ( WSDL)?
  • Is there something I am missing to squeeze correctly HttpWebRequest?
  • Perhaps something is going wrong that will return an error message?
    I expect another authentication-related message to be invalid if the request itself was ok.
  • Is there any way to compress using app.config?

. , / . , ContentType , ( ) , content-transfer-encoding ? ContentType, ?

SOAP Evenlope MTOM "application/xop + xml", 8 .

, , , . , , , .

HttpWebRequest:

private static HttpWebRequest CreateWebRequest(SoapAction action)
{
    string url = GetUrlAddress(action);

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

    request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
    request.Headers.Add("SOAPAction", action.ToString());
    request.ContentType = "application/xop+xml";
    request.Accept = "text/xml";
    request.Method = "POST";

    request.ClientCertificates.Add(/* Retrieve X509Certificate Object*/);

    return request;
}

:

using (Stream stream = request.GetRequestStream())
{
    soapXml.Save(stream);
}

:
,

try
{
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (StreamReader reader = new StreamReader(response, Encoding.Default))
        {
            File.AppendAllText(filePath, response.Headers.ToString());
            File.AppendAllText(filePath, reader.ReadToEnd());
        }
    }
}
catch (WebException ex)
{
    using (var stream = ex.Response.GetResponseStream())
    {
        using (var reader = new StreamReader(stream))
        {
            Console.WriteLine(reader.ReadToEnd());
        }
    }
}

:

HTTP- (RFC 1952 - GZIP).

+1
2

, , HttpWebRequest:

request.Headers.Add(HttpRequestHeader.ContentEncoding, "gzip"); 

HttpWebRequest:

private static HttpWebRequest CreateWebRequest(SoapAction action)
{
    string url = GetUrlAddress(action);

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

    request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
    request.Headers.Add(HttpRequestHeader.ContentEncoding, "gzip"); 
    request.Headers.Add("SOAPAction", action.ToString());
    request.ContentType = "application/xop+xml";
    request.Accept = "text/xml";
    request.Method = "POST";

    request.ClientCertificates.Add(/* Retrieve X509Certificate Object*/);

    return request;
}
+2

.

using (Stream stream = request.GetRequestStream())
using (GZipStream gz = new GZipStream(stream, CompressionMode.Compress, false))
{
    soapXml.Save(gz);
}

: , " GZipStream (, CompressionMode.Compress, true)". , -.

0

Source: https://habr.com/ru/post/1626657/


All Articles