如何使用 C# 和 IIS 无缝压缩发布到表单的数据?

发布于 2024-07-10 18:18:58 字数 1537 浏览 8 评论 0原文

我必须与一个不使用网络服务的稍微陈旧的系统进行交互。 为了将数据发送到该系统,我需要将 XML 文档发布到其他系统网站上的表单中。 该 XML 文档可能会变得非常大,因此我想对其进行压缩。 另一个系统位于 IIS 上,我使用 C#。 我当然可以实现一些在发布数据之前压缩数据的方法,但这需要其他系统进行更改,以便它可以解压缩数据。 我想避免更改其他系统,因为我不拥有它。

我听说过有关在 IIS 和浏览器中启用压缩/http 1.1 的模糊说法,但我不知道如何将其转换为我的程序。 基本上,我是否可以在程序中设置一些属性,使我的程序自动压缩发送到 IIS 的数据,并让 IIS 无缝解压缩数据,以便接收应用程序甚至不知道其中的区别?

这是一些示例代码,大致展示了我在做什么;

private static void demo()
    {

        Stream myRequestStream = null;
        Stream myResponseStream = null;

        HttpWebRequest myWebRequest = (HttpWebRequest)System.Net
                              .WebRequest.Create("http://example.com");
        byte[] bytMessage = null;
        bytMessage = Encoding.ASCII.GetBytes("data=xyz");
        myWebRequest.ContentLength = bytMessage.Length;
        myWebRequest.Method = "POST";

        // Set the content type as form so that the data
        // will be posted as form              
        myWebRequest.ContentType = "application/x-www-form-urlencoded";

        //Get Stream object 
        myRequestStream = myWebRequest.GetRequestStream();

        //Writes a sequence of bytes to the current stream    
        myRequestStream.Write(bytMessage, 0, bytMessage.Length);

        //Close stream
        myRequestStream.Close();

        WebResponse myWebResponse = myWebRequest.GetResponse();
        myResponseStream = myWebResponse.GetResponseStream();
}

“data=xyz”实际上是“data=[一个几MB的XML文档]”。

我知道,如果可以通过非编程手段实现这个问题,那么这个问题最终可能会落入非编程的旗帜下,所以提前道歉。

I have to interface with a slightly archaic system that doesn't use webservices. In order to send data to this system, I need to post an XML document into a form on the other system's website. This XML document can get very large so I would like to compress it.
The other system sits on IIS and I use C# my end. I could of course implement something that compresses the data before posting it, but that requires the other system to change so it can decompress the data. I would like to avoid changing the other system as I don't own it.

I have heard vague things about enabling compression / http 1.1 in IIS and the browser but I have no idea how to translate that to my program. Basically, is there some property I can set in my program that will make my program automatically compress the data that it is sending to IIS and for IIS to seamlessly decompress it so the receiving app doesn't even know the difference?

Here is some sample code to show roughly what I am doing;

private static void demo()
    {

        Stream myRequestStream = null;
        Stream myResponseStream = null;

        HttpWebRequest myWebRequest = (HttpWebRequest)System.Net
                              .WebRequest.Create("http://example.com");
        byte[] bytMessage = null;
        bytMessage = Encoding.ASCII.GetBytes("data=xyz");
        myWebRequest.ContentLength = bytMessage.Length;
        myWebRequest.Method = "POST";

        // Set the content type as form so that the data
        // will be posted as form              
        myWebRequest.ContentType = "application/x-www-form-urlencoded";

        //Get Stream object 
        myRequestStream = myWebRequest.GetRequestStream();

        //Writes a sequence of bytes to the current stream    
        myRequestStream.Write(bytMessage, 0, bytMessage.Length);

        //Close stream
        myRequestStream.Close();

        WebResponse myWebResponse = myWebRequest.GetResponse();
        myResponseStream = myWebResponse.GetResponseStream();
}

"data=xyz" will actually be "data=[a several MB XML document]".

I am aware that this question may ultimately fall under the non-programming banner if this is achievable through non-programmatic means so apologies in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神仙妹妹 2024-07-17 18:18:59

我认为没有办法在一侧压缩数据并在另一侧接收未压缩的数据而不主动解压缩数据。

I see no way to compress the data on one side and receiving them uncompressed on the other side without actively uncompressing the data..

墨落成白 2024-07-17 18:18:59

不知道这是否可行,因为我能找到的所有示例都可供下载,但您可以尝试使用 gzip 来压缩数据,然后将传出消息上的 Content-Encoding 标头设置为 gzip。 我认为长度应该是压缩消息的长度,尽管如果这不起作用,您可能想尝试将其设置为未编码消息的长度。

祝你好运。

编辑 我认为问题在于支持压缩的 ISAPI 过滤器是否在上传时/始终/可配置地调用。 我找不到答案,所以我怀疑答案是永远不会,但除非你尝试(或找到我没有找到的答案),否则你不会知道。

No idea if this will work since all of the examples I could find were for download, but you could try using gzip to compress the data, then set the Content-Encoding header on the outgoing message to gzip. I believe that the Length should be the length of the zipped message, although you may want to play with making it the length of the unencoded message if that doesn't work.

Good luck.

EDIT I think the issue is whether the ISAPI filter that supports compression is ever/always/configurably invoked on upload. I couldn't find an answer to that so I suspect that the answer is never, but you won't know until you try (or find the answer that eluded me).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文