在哪里设置服务引用上的 CookieContainer?

2024-04-19

例如,当将 WebService 引用添加到 .NET 2.0 项目上的 ASMX 服务时,

var objService = new NameSpace.groupservices();

那里存在,

objService.CookieContainer = new System.Net.CookieContainer();

例如,当将 ServiceReference 添加到 .NET 4.0 项目上的 ASMX 服务时,

var objService = new NameSpace.groupservicesSoapClient();

objService 没有任何 CookieContainer 属性

有人问了类似的问题here https://stackoverflow.com/questions/1777221/using-cookiecontainer-with-webclient-class没有正解。

有人可以指导在哪里可以找到该房产吗?


与绑定到 HTTP 传输的 ASMX Web 服务相比,WCF 允许使用各种传输协议。因此,并非所有特定于协议的选项(例如用于 HTTP 传输的 Cookie)在 WCF 服务引用中都可用。

但是,您可以添加一个消息检查器来检查客户端和服务器之间发送的消息。这article http://www.codeproject.com/Articles/190806/Send-Cookies-When-Making-WCF-Service-Calls描述了一种向服务器发送cookie的方法。

我已扩展示例以使用 CookieContainer。另外,以下代码显示了如何评估Set-Cookie服务器发送的标头,用于将新的 cookie 添加到容器中。请注意,该示例显示了基本轮廓,但可能需要扩展或更多验证。然而,在一个简单的场景中它是有效的。

以下代码片段显示了托管在 IIS 上并集成在 ASP.NET 框架中的 WCF 服务的测试方法。它基本上以字符串形式回显发送到服务器的 cookie,并添加两个新的 cookie:

public string GetData(int value)
{
    var reply = string.Join(", ", 
                    from x in HttpContext.Current.Request.Cookies.AllKeys 
                    select x + "=" + HttpContext.Current.Request.Cookies[x].Value);
    HttpContext.Current.Response.Cookies.Add(new HttpCookie("Test", "Test123"));
    HttpContext.Current.Response.Cookies.Add(new HttpCookie("Test2", "Test1234"));
    return reply;
}

以下测试程序为 cookie 创建 CookieContainer,添加演示 cookie 并为服务端点注册新行为:

class Program
{
    static void Main(string[] args)
    {
        var cookieCont = new CookieContainer();
        using(var svc = new TestServiceReference.TestServiceClient())
        {
            cookieCont.Add(svc.Endpoint.Address.Uri, new Cookie("TestClientCookie", "Cookie Value 123"));
            var behave = new CookieBehavior(cookieCont);
            svc.Endpoint.EndpointBehaviors.Add(behave);
            var data = svc.GetData(123);
            Console.WriteLine(data);
            Console.WriteLine("---");
            foreach (Cookie x in cookieCont.GetCookies(svc.Endpoint.Address.Uri))
                Console.WriteLine(x.Name + "=" + x.Value);
        }
        Console.ReadLine();
    }
}

该行为的目的是添加自定义消息检查器并移交 CookieContainer:

public class CookieBehavior : IEndpointBehavior
{
    private CookieContainer cookieCont;

    public CookieBehavior(CookieContainer cookieCont)
    {
        this.cookieCont = cookieCont;
    }

    public void AddBindingParameters(ServiceEndpoint serviceEndpoint,
        System.ServiceModel.Channels
        .BindingParameterCollection bindingParameters) { }

    public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint,
        System.ServiceModel.Dispatcher.ClientRuntime behavior)
    {
        behavior.MessageInspectors.Add(new CookieMessageInspector(cookieCont));
    }

    public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint,
        System.ServiceModel.Dispatcher
        .EndpointDispatcher endpointDispatcher) { }

    public void Validate(ServiceEndpoint serviceEndpoint) { }
}

当请求发送到服务器时,消息检查器都会添加 cookieBeforeSendRequest方法并检索应在中更新的cookieAfterReceiveReply方法。请注意,correlationState由返回BeforeSendRequest用于检索中的 UriAfterReceiveReply:

public class CookieMessageInspector : IClientMessageInspector
{
    private CookieContainer cookieCont;

    public CookieMessageInspector(CookieContainer cookieCont)
    {
        this.cookieCont = cookieCont;
    }

    public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply,
        object correlationState) 
    {
        object obj;
        if (reply.Properties.TryGetValue(HttpResponseMessageProperty.Name, out obj))
        {
            HttpResponseMessageProperty httpResponseMsg = obj as HttpResponseMessageProperty;
            if (!string.IsNullOrEmpty(httpResponseMsg.Headers["Set-Cookie"]))
            {
                cookieCont.SetCookies((Uri)correlationState, httpResponseMsg.Headers["Set-Cookie"]);
            }
        }
    }

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request,
        System.ServiceModel.IClientChannel channel)
    {
        object obj;
        if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out obj))
        {
            HttpRequestMessageProperty httpRequestMsg = obj as HttpRequestMessageProperty;
            SetRequestCookies(channel, httpRequestMsg);
        }
        else
        {
            var httpRequestMsg = new HttpRequestMessageProperty();
            SetRequestCookies(channel, httpRequestMsg);
            request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMsg);
        }

        return channel.RemoteAddress.Uri;
    }

    private void SetRequestCookies(System.ServiceModel.IClientChannel channel, HttpRequestMessageProperty httpRequestMessage)
    {
        httpRequestMessage.Headers["Cookie"] = cookieCont.GetCookieHeader(channel.RemoteAddress.Uri);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在哪里设置服务引用上的 CookieContainer? 的相关文章

随机推荐

  • 在 JavaScript 中将浮点数转换为整数的最佳方法是什么?

    在 JavaScript 中 有多种不同的方法可以将浮点数转换为整数 我的问题是哪种方法可以提供最佳性能 最兼容或被认为是最佳实践 以下是我所知道的几种方法 var a 2 5 window parseInt a 2 Math floor
  • 如何在 Android 中强制蓝牙 LE“正常工作”配对

    我有一个运行 GATT 服务器的嵌入式系统 我尝试通过 Android 连接该服务器 任务很简单 连接到 GATT 服务器 导航特征并验证读 写功能 问题是当我尝试连接 Android 应用程序时 它尝试与 Passkey 而不是 Just
  • Node.js 中硬编码的 mysql 用户和密码

    我将 mysql 与 node js 一起使用 像这样的东西 var mysql require mysql var connection mysql createConnection host localhost user me pass
  • Python 3 中的 FastCGI WSGI 库?

    对于 Python 3 是否存在可以将 WSGI 应用程序用作 FastCGI 服务器的库 这样 nginx 就可以将请求代理给它了 Python 3 文档 https docs python org 3 howto webservers
  • 重定向子进程标准输出

    我已经使用 test py 中的 Redir 类设置了标准输出重定向 如下 输出应在文本框中显示两个打印语句 但目前只有 Output1 被发送到文本框 Output2 在后面的控制台中打印 我想知道是否有办法重定向子进程的标准输出 我尝试
  • 启动套接字服务器会干扰 gRPC/http 客户端服务器通信 Golang

    我有一个很大程度上基于此的服务器tutorial https www golinuxcloud com go grpc crud api postgresql db 在我对其应用了额外的更改后 它工作得很好 但现在我想添加socket io
  • Java“重复局部变量” - 是在 Java 还是 Eclipse 中抛出错误?

    在下面的代码中 private static void example String inputString test switch inputString case test String outputString The string
  • 如何访问 Pyramid .ini 文件中的自定义部分?

    我目前正在为多种服务编写数据收集服务 可能有 5 个不同的 API 端点 具有不同的主机和端口号 我想为此创建一个设置文件 但认为 ini应该是一个更好的地方或者我是这么想的 我的development ini 看起来像这样 app mai
  • 如何让 Symfony 2 采用协议方案(http vs https)

    我有一个 Symfony 2 网站 在开发中运行在 HTTP 上 在生产中运行在 HTTPS 上 我注意到在生产中 Symfony 生成的 URL 仍然全部呈现为 HTTP 我怎么也可以 让框架采用当前提供网站的协议 可能是首选 或者 仅在
  • 如何检查视频文件是否大于2MB?

    假设我从 iPhone 库中获取了一个视频文件 我想检查视频文件不应大于 2MB 我无法使用 videoMaximumDuration 方法 因为如果任何视频是高清质量的 即使是 1 分钟持续时间的视频也可能会很大 有什么意见吗 urlvi
  • 为整个服务器/域强制使用 https

    我正在开发一些只能通过 https 访问的表单 我有一个专用服务器 有自己的证书和所有好东西 所以我的问题实际上有两个 1 强制每个请求都为 https 的最佳方法是什么 有没有比这个 htacess mod rewrite 规则更好的方法
  • Gmaps.js 停止工作

    从一天到另一天 Gmaps js 库停止工作 我创建了一张带有 3 个标记的地图 他们有 InfoWindows 我在 InfoWindow 中添加了一个小路由选项 您可以在其中输入您的地址中 Gmaps 将您引导至该点 现在 在过去两周内
  • __non_webpack_require__ 未定义

    我是 webpack 和 node 的新手 我想知道如何使用 non webpack require 功能 我去过webpack 的网站 https webpack js org api module variables non webpa
  • Azure 函数 python 命名参数没有值

    我目前正在 azure 函数中使用 python 创建一个计时器触发器 该触发器聚合来自 blob 存储的数据 并将结果放入 cosmosDB 中 我的问题如下 当我在绑定路径中使用特定文件时 函数按预期运行 每当我更改它 以便获取容器中的
  • 如何在 PHP 5.3+ 中的命名空间类内部使用全局命名空间类型提示?

    namespace MyClass Util class Sample public function each Object f 来自调用文件 未命名空间 sample new Sample sample gt each new stdC
  • 如何使用 mechanize 获取生成的验证码图像

    我正在尝试使用 python 和 mechanize 从我的移动提供商网站发送短信 问题是表单有验证码图像 使用 mechanize 我可以获取图像的链接 但每次访问该链接时它都是不同的 有什么办法可以从 mechanize 获得准确的图片
  • 执行某些命令(如“prune”)时,Wincred 无法与 Git Bash(Git for Windows)正常工作

    我已经在 Windows 7 64 位中很好地设置了 GitforWindows 并将凭据管理器设置为 Wincred 然而 当我运行一些命令时 比如git remote prune origin在 GitBash 中 尽管运行命令 但它在
  • 在 Power BI 自定义视觉对象中使用 d3.js 库绘制一条线

    我正在努力在 Power BI 自定义视觉对象中绘制一条单线 Power BI 中的报表是使用 TypeScript 和 d3 js v 3 0 编写的 我可以用轴绘制图表 但没有出现线条 在 HTML 文件中使用纯 d3 js 确实很容易
  • wxHTTP 和线程

    我在线程内使用 wxHTTP 时遇到一些问题 我创建了以下从 wxThread 派生的类来使用 wxHTTP class Thread public wxThread private wxHTTP get public Thread Thr
  • 在哪里设置服务引用上的 CookieContainer?

    例如 当将 WebService 引用添加到 NET 2 0 项目上的 ASMX 服务时 var objService new NameSpace groupservices 那里存在 objService CookieContainer