[英]How do I prevent my .NET SOAP client from including “Connection: KeepAlive” in the HTTP headers. (using WSE3.0)
In the HTTP Connection header, my web service client is including: Connection: Keep-Alive
在HTTP Connection标头中,我的Web服务客户端包括:
Connection: Keep-Alive
I want to disable this. 我想禁用它。
After doing some research, it appears the way to do this is to set the KeepAlive member of the
SoapHttpChannelOptions
class to false. 在做了一些研究后,看来这样做的方法是将
SoapHttpChannelOptions
类的KeepAlive成员设置为false。 But, I do not see a way to access/modify
SoapHttpChannelOptions
in the webservice client class that was generated for me in Visual Studio using WSE3.0 (Web Service Enhancement. 但是,我没有看到在使用WSE3.0(Web服务增强)在Visual Studio中为我生成的Web服务
客户端类中
访问/修改
SoapHttpChannelOptions
的方法 。
In my case, the generated stub class extends Microsoft.Web.Services3.WebServicesClientProtocol
在我的例子中,生成的存根类扩展了
Microsoft.Web.Services3.WebServicesClientProtocol
I've been unable to find any examples searching google and most members of the SoapHttpChannelOptions class are inherited into the WebServicesClientProtocol class... 我一直无法找到任何搜索谷歌的例子,SoapHttpChannelOptions类的大多数成员都被继承到WebServicesClientProtocol类......
SoapHttpChannelOptions Reference SoapHttpChannelOptions参考
WebServicesClientProtocol Reference WebServicesClientProtocol Reference
Note: I'm not trying to modify the web server. 注意:我不是要修改Web服务器。 I'm trying to modify the web service client.
我正在尝试修改Web服务客户端。
One solution is to override the GetWebRequest(Uri uri)
method. 一种解决方案是覆盖
GetWebRequest(Uri uri)
方法。
The information which lead me to this solution was found on this MSDN Forum Post 在这个MSDN论坛帖子上找到了引导我找到这个解决方案的信息
Paste this snippet into the Reference.cs file which was automatically generated for you. 将此代码段粘贴到自动为您生成的Reference.cs文件中。 The downside of this approach is if you ever regenerate the web service client adapters (ie Update Web References) you will have to then modify the file again.
这种方法的缺点是,如果您重新生成Web服务客户端适配器(即更新Web引用),则必须再次修改该文件。
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
webRequest.KeepAlive = false;
return webRequest;
}
Create a file and paste the following code into it. 创建一个文件并将以下代码粘贴到其中。 Modify the namespace and class name so they match your webservice.
修改名称空间和类名,使它们与您的Web服务匹配。
namespace YourNamespace
{
using System.Diagnostics;
using System.Web.Services;
using System.ComponentModel;
using System.Web.Services.Protocols;
using System;
using System.Xml.Serialization;
/// <summary>
/// This partial class makes it so all requests specify
/// "Connection: Close" instead of "Connection: KeepAlive" in the HTTP headers.
/// </summary>
public partial class YourServiceNameWse : Microsoft.Web.Services3.WebServicesClientProtocol
{
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
webRequest.KeepAlive = false;
return webRequest;
}
}
}
This approach may work if you do not use WSE. 如果您不使用WSE,此方法可能有效。 I was able to paste the method above under the non WSE webservice class... which extends
System.Web.Services.Protocols.SoapHttpClientProtocol
. 我能够将上面的方法粘贴到非WSE webservice类下...它扩展了
System.Web.Services.Protocols.SoapHttpClientProtocol
。 From my testing it appeared that this made it not include any Http Connection line at all where as when I did it inside a WSE class (which are derived from Microsoft.Web.Services3.WebServicesClientProtocol
) it then included a "Connection: Close" line. 从我的测试看来,这使得它不包括任何Http连接线,就像我在WSE类(它是从
Microsoft.Web.Services3.WebServicesClientProtocol
派生)中那样,它然后包含一个“连接:关闭”行。 According to this site on HTTP KeepAlive : 根据HTTP KeepAlive上的这个网站 :
Under HTTP 1.1, the official keepalive method is different.
在HTTP 1.1下,官方的keepalive方法是不同的。 All connections are kept alive, unless stated otherwise with the following header: Connection: close
除非另有说明,否则所有连接都保持活动状态:连接:关闭
So, while it may not include KeepAlive in the header anymore... I think in HTTP1.1 it's assumed to be the default. 所以,虽然它可能不再在标题中包含KeepAlive ......我认为在HTTP1.1中它被认为是默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.