Java 与 Net HTTP 客户端性能对比

发布于 2024-12-15 12:38:21 字数 3787 浏览 0 评论 0原文

我们从 C# 应用程序调用 Web 服务,使用 WCF (BasicHttpBinding) 大约需要 300 毫秒。我们注意到,从 SOAP UI 发送相同的 SOAP 调用只需要大约 30 毫秒。

现在,我们还实现了通过基本 WebClient 访问 Web 服务的测试,以确保 WCf 的 DeSer 部分不是造成此额外延迟的原因。当使用 WebClient 类时,调用也需要大约 300 毫秒。

关于为什么 Java 在这方面比 C# 快 10 倍有什么想法吗? .NET 方面是否可以进行某种调整?

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        executeTest(() =>
            {
                var resultObj = client.getNextSeqNr(new WcfClient()
                {
                    domain = "?",
                    hostname = "?",
                    ipaddress = "?",
                    loginVersion = "?",
                    processId = "?",
                    program = "?",
                    userId = "?",
                    userIdPw = "?",
                    userName = "?"
                }, "?", "?");
            });
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        WebClient webClient = new WebClient();

        executeTest(()=>
            {
                webClient.Proxy = null;
                webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
                webClient.Headers.Add("Content-Type", "application/xml");
                webClient.Encoding = Encoding.UTF8;
                var data = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"SomeNamespace\">" +
                            "   <soapenv:Header/>" +
                            "   <soapenv:Body>" +
                            "      <ser:getNextSeqNr>" +
                            "         <!--Optional:-->" +
                            "         <clientInfo>" +
                            "            <!--Optional:-->" +
                            "            <domain>?</domain>" +
                            "            <!--Optional:-->" +
                            "            <hostname>?</hostname>" +
                            "            <!--Optional:-->" +
                            "            <ipaddress>?</ipaddress>" +
                            "            <!--Optional:-->" +
                            "            <loginVersion>?</loginVersion>" +
                            "            <!--Optional:-->" +
                            "            <processId>?</processId>" +
                            "            <!--Optional:-->" +
                            "            <program>?</program>" +
                            "            <!--Optional:-->" +
                            "            <userId>*</userId>" +
                            "            <!--Optional:-->" +
                            "            <userIdPw>?</userIdPw>" +
                            "            <!--Optional:-->" +
                            "            <userName>?</userName>" +
                            "         </clientInfo>" +
                            "         <!--Optional:-->" +
                            "         <name>?</name>" +
                            "         <!--Optional:-->" +
                            "         <schema>?</schema>" +
                            "      </ser:getNextSeqNr>" +
                            "   </soapenv:Body>" +
                            "</soapenv:Envelope>";
                string result = webClient.UploadString("http://server:8080/service", "POST", data);
            });
    }

我在这里错过了什么吗?任何想法都会有帮助...;-)

亲切的问候, 塞巴斯蒂安

We call a webservice from our C# app which takes about 300ms using WCF (BasicHttpBinding). We noticed that the same SOAP call does only take about 30ms when sending it from SOAP UI.

Now we also implemented a test accessing the webservice via a basic WebClient in order to make sure that the DeSer-part of the WCf is not the reason for this additional delay. When using the WebClient class the call takes about 300ms as well.

Any ideas on why Java compared to C# is about 10x faster in this regard? Is there some kind of tweaking possible on the .NET side of things?

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        executeTest(() =>
            {
                var resultObj = client.getNextSeqNr(new WcfClient()
                {
                    domain = "?",
                    hostname = "?",
                    ipaddress = "?",
                    loginVersion = "?",
                    processId = "?",
                    program = "?",
                    userId = "?",
                    userIdPw = "?",
                    userName = "?"
                }, "?", "?");
            });
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        WebClient webClient = new WebClient();

        executeTest(()=>
            {
                webClient.Proxy = null;
                webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
                webClient.Headers.Add("Content-Type", "application/xml");
                webClient.Encoding = Encoding.UTF8;
                var data = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"SomeNamespace\">" +
                            "   <soapenv:Header/>" +
                            "   <soapenv:Body>" +
                            "      <ser:getNextSeqNr>" +
                            "         <!--Optional:-->" +
                            "         <clientInfo>" +
                            "            <!--Optional:-->" +
                            "            <domain>?</domain>" +
                            "            <!--Optional:-->" +
                            "            <hostname>?</hostname>" +
                            "            <!--Optional:-->" +
                            "            <ipaddress>?</ipaddress>" +
                            "            <!--Optional:-->" +
                            "            <loginVersion>?</loginVersion>" +
                            "            <!--Optional:-->" +
                            "            <processId>?</processId>" +
                            "            <!--Optional:-->" +
                            "            <program>?</program>" +
                            "            <!--Optional:-->" +
                            "            <userId>*</userId>" +
                            "            <!--Optional:-->" +
                            "            <userIdPw>?</userIdPw>" +
                            "            <!--Optional:-->" +
                            "            <userName>?</userName>" +
                            "         </clientInfo>" +
                            "         <!--Optional:-->" +
                            "         <name>?</name>" +
                            "         <!--Optional:-->" +
                            "         <schema>?</schema>" +
                            "      </ser:getNextSeqNr>" +
                            "   </soapenv:Body>" +
                            "</soapenv:Envelope>";
                string result = webClient.UploadString("http://server:8080/service", "POST", data);
            });
    }

Am I missing something here? Any idea would be helpful... ;-)

Kind regards,
Sebastian

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

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

发布评论

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

评论(1

初相遇 2024-12-22 12:38:21

我刚刚找到了这个原因。

它是 100-Expect Continue HTTP 标头以及 .NET 中的相应实现。 .NET 客户端默认在服务器上等待 350 毫秒。这会导致延误。 Java 似乎还有其他默认值...

只需在代码的早期添加以下代码行:

System.Net.ServicePointManager.Expect100Continue = false;

干杯!

I just found the reason for this.

It's the 100-Expect Continue HTTP Header and the corresponding implementation in .NET. The .NET client wait 350ms as default on the server. This causes the delays. Java seems to have other default values here...

Just add the following line of code very early in your code:

System.Net.ServicePointManager.Expect100Continue = false;

Cheers!

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