简体   繁体   English

如何防止我的.NET SOAP客户端在HTTP标头中包含“Connection:KeepAlive”。 (使用WSE3.0)

[英]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论坛帖子上找到了引导我找到这个解决方案的信息

Method 1: Modify the Auto Generated file. 方法1:修改自动生成的文件。

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

Method 2: Create a partial class 方法2:创建一个分部类

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

Notes 笔记

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.

相关问题 如何在WSE 3.0(.NET)中的UsernameToken的Nonce元素中添加EncodingType属性 - How do I add an EncodingType attribute to the Nonce element of a UsernameToken in WSE 3.0 (.NET) 我怎么知道我的项目正在使用哪个版本的WSE? - How do I know which version of WSE my project is using? 如何使用.net webservices wse或wcf创建安全的SOAP主体? - How to create secure SOAP body using .net webservices wse or wcf? 如何防止 DateTime 在 SOAP xsd:dateTime 元素中包含区域偏移量? - How do I prevent DateTime from including zone offset in SOAP xsd:dateTime element? 如何使用WCF(或WSE 3)在Web服务客户端应用程序的请求和响应中混合消息编码类型(Text / MTOM)? - How do I mix message encoding types (Text/MTOM) in the Request & Response of a Web Service client application using WCF (or WSE 3)? 使用证书签署SOAP(WSE) - Sign SOAP using certificate (WSE) 从JAXWS-RI调用.NET Web服务(WSE 3.0,WS-Security) - Calling a .NET web service (WSE 3.0, WS-Security) from JAXWS-RI 使用MTOM和WSE 3选择性编码SOAP节 - Selectively encoding SOAP Sections using MTOM and WSE 3 如何在WebMatrix中创建Soap Client - How do I create a Soap Client in WebMatrix WSE 3.0信任链错误-如何查看发回的证书? 我想知道哪个证书存在信任链错误 - WSE 3.0 Trust chain error - how do i view what certificate is being sent back? I want to know what cert has the trust chain error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM