使用gzip压缩HttpWebRequest [英] Compress a HttpWebRequest using gzip

查看:279
本文介绍了使用gzip压缩HttpWebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发.NET 4.0控制台应用程序以用作SOAP Web Service客户端,该客户端将通过数据发送(POST)到第三方.我无法控制服务器端的Web服务.第三方确实提供了WSDL's可供使用,并且我能够导入它们并成功地使用它们.但是,需要使用gzip压缩请求消息,我一生都无法弄清楚如何使用代理服务来做到这一点.

I am developing a .NET 4.0 Console Application to serve as a SOAP Web Service client which will send data (POST) through to a third-party. I have no control over the web service on the server-side. The third-party did provide WSDL's to 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 I could not for the life of me figure out how to do that using the proxy services.

本主题在SO上,这使我相信,如果无法控制客户端和服务器代码,则无法压缩请求.这项发现的结果是,我在应用程序中编写了代码,以便在XDocument对象中手动创建SOAP XML.然后,填充WSDL代理类对象中的值,这些对象以前已编码为我的客户端应用程序使用.

This topic here on SO, led me to believe that without having control over both the client and server code, the request is unable to be compressed. As a result of this finding, I wrote the code in my application to manually create the SOAP XML in a XDocument object; then, populated the values from the WSDL proxy class objects which I had previously coded my client application to use.

此客户端的第一个要求是发送通过gzip压缩的消息.经过研究,我发现答案很简单,只需将HttpRequestHeader.AcceptEncoding, "gzip, deflate"添加到请求标头即可.不幸的是,这样做似乎行不通.

The first requirement for this client, is to send the message compressed via gzip. After some research, I have seen answers as simple as just adding the HttpRequestHeader.AcceptEncoding, "gzip, deflate" to the request header. Unfortunately, doing that that didn't appear to work.

当前,我正在检索的证书不是真实的证书.在尝试部署到测试环境以进行实际的服务测试之前,我试图使代码尽可能地合理.

Currently, the certificate which I am retrieving is not the real certificate. I am trying to make the code as sound as I can before deploying to a test environment to do the actual service testing.

  1. 是否可以通过代理调用(wsdl)压缩请求?
  2. 要正确压缩HttpWebRequest,我是否缺少某些东西?
  3. 可能还有其他问题会导致返回错误消息吗?
    如果请求本身可以,我希望有关身份验证的其他消息无效.
  4. 是否可以通过app.config完成压缩?
  1. Is there a way to compress the request via the proxy call (wsdl)?
  2. Is there something I am missing in order to properly compress the HttpWebRequest?
  3. Might there be something else going wrong which would result in the error message to be returned?
    I would expect a different message pertaining to authentication being invalid if the request itself was OK.
  4. Is there a way the compression can be done through the app.config?

我对下一组要求在如何处理/做什么方面有些困惑.在我将请求的ContentType设置为什么的假设下,如何添加(和添加)以便将content-transfer-encoding片段添加到请求中?如果ContentType错误,我该如何添加此信息?

The next set of requirements I'm a little confused on how to handle/what to do. Under the assumption that what I have set the ContentType of the request to, how (and what) do I add in order to add the content-transfer-encoding piece to the request? If the ContentType is incorrect, how should I add this information as well?

带有MTOM编码附件的SOAP Evenlope的内容类型必须为"application/xop + xml",并且内容传输编码必须为8位.

The content type for the SOAP Evenlope with MTOM encoded attachment must be "application/xop+xml" and the content-transfer-encoding must be 8-bit.

我已经看了下面的代码的一些迭代,但是,我相信相关的代码片段是最简单形式的代码.请让我知道是否还有其他有用的信息.

I've gone through a few iterations of the code below, however, I believe the relevant snippets are the code at its simplest form. Please let me know if there is other information that would be helpful.

创建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)发送请求消息.

The request message must be sent using HTTP compression (RFC 1952 - GZIP).

推荐答案

我认为我可以通过将以下内容添加到HttpWebRequest方法来解决压缩错误消息:

I think I was able to solve the compression error message by adding the following to the HttpWebRequest method:

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;
}

这篇关于使用gzip压缩HttpWebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆