简体   繁体   English

如何配置C#Web服务客户端以并行发送HTTP请求标头和正文?

[英]How do I configure a C# web service client to send HTTP request header and body in parallel?

I am using a traditional C# web service client generated in VS2008 .Net 3.5, inheriting from SoapHttpClientProtocol. 我正在使用VS2008 .Net 3.5中生成的传统C#Web服务客户端,该客户端继承自SoapHttpClientProtocol。 This is connecting to a remote web service written in Java. 这正在连接到用Java编写的远程Web服务。

All configuration is done in code during client initialization, and can be seen below: 所有配置均在客户端初始化期间通过代码完成,如下所示:

        ServicePointManager.Expect100Continue = false;
        ServicePointManager.DefaultConnectionLimit = 10;

        var client = new APIService
        {
            EnableDecompression = true,
            Url = _url + "?guid=" + Guid.NewGuid(),
            Credentials = new NetworkCredential(user, password, null),
            PreAuthenticate = true,
            Timeout = 5000 // 5 sec
        };

It all works fine, but the time taken to execute the simplest method call is almost double the network ping time. 一切正常,但是执行最简单的方法调用所需的时间几乎是网络ping时间的两倍。 Whereas a Java test client takes roughly the same as the network ping time: 而Java测试客户端所花费的时间与网络ping时间大致相同:

C# client ~ 550ms
Java client ~ 340ms
Network ping ~ 300ms

After analyzing the TCP traffic for a session discovered the following: 在分析会话的TCP通信量之后,发现了以下内容:

Basically, the C# client sent TCP packets in the following sequence. 基本上,C#客户端按以下顺序发送TCP数据包。

Client Send HTTP Headers in one packet.
Client Waits For TCP ACK from server.
Client Sends HTTP Body in one packet.
Client Waits For TCP ACK from server.

The Java client sent TCP packets in the following sequence. Java客户端按以下顺序发送TCP数据包。

Client Sends HTTP Headers in one packet.
Client Sends HTTP Body in one packet.
Client Revieves ACK for first packet.
Client Revieves ACK for second packet.
Client Revieves ACK for second packet.

Is there anyway to configure the C# web service client to send the header/body in parallel as the Java client appears to? 无论如何,有没有配置C#Web服务客户端以像Java客户端那样并行发送标头/正文?

Any help or pointers much appreciated. 任何帮助或指针,不胜感激。

Thanks for the reply Rob, eventually I opted to use the Add Service Reference / WCF proxy generation, which does this by default. 感谢Rob的答复,最终我选择使用“添加服务参考” / WCF代理生成,默认情况下会这样做。 Probably because it's using newer HTTP libraries underneath. 可能是因为它在下面使用了更新的HTTP库。

I did have a few WCF proxy generation issues with SOAP methods that return raw arrays of complex objects (ie: returning an object that contains an array of objects worked fine). 我确实遇到了一些WCF代理生成问题,这些问题与返回复杂对象的原始数组的SOAP方法(即:返回包含对象数组的对象工作正常)有关。 To get round this you either have to wrap your arrays in objects, or switch the SOAP server config from RPC to DOCUMENT (which is what we did). 为了解决这个问题,您要么将数组包装在对象中,要么将SOAP服务器配置从RPC切换到DOCUMENT(这就是我们所做的)。

I think you can use the inherited EndGetRequestStream method to hack the SoapHttpClientProtocol. 我认为您可以使用继承的EndGetRequestStream方法来破解SoapHttpClientProtocol。 Save that to a buffer until the request has finished. 将其保存到缓冲区,直到请求完成。 Then make your own stream and push it all out at once. 然后制作自己的视频流,并立即将其全部推送出去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#Web服务客户端:如何添加自定义标头以进行请求? - c# web service client: how to add custom header to request? 如何为使用Axis 1.4 Web服务的C#Web服务客户端添加自定义Http Header - How to add custom Http Header for C# Web Service Client consuming Axis 1.4 Web service 将C#请求Web服务与标头和正文一起使用 - Use C# request web service with header and body 如何修改请求的http头; C#中的Web参考 - how to modify http header of request; web reference in C# 如何从Java客户端向C#Web服务发送数据 - How to send data to c# web service from java client 在请求正文中发送参数和文件,带有源请求 Header C# - Send parameters & file in Request Body with Origin Request Header C# 如何将HTTP标头添加到Web服务调用? - How do I add an HTTP header to a web service call? 如何在C#中的GraphQL Client端点的请求主体中发送oauth_token和client_id - How to send oauth_token and client_id in the request body of GraphQL Client endpoint in c# 您如何从ac#客户端发送补丁请求? - How do you send a patch request from a c# client? 在C#中检索Http客户端的POST请求正文 - Retrieve POST Request Body of Http client in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM