ASP.NET Core 使用 WCF 客户端发出 SOAP API 请求如何向请求添加 Cookie 标头?

Pet*_*ter 2 api wcf soap session-cookies asp.net-core

所以我目前正在使用 WCF 生成的代码“客户端对象”向服务发出 SOAP API 请求,我想知道如何将 Cookie 标头设置为请求?

Abr*_*ian 5

一般来说,我们通过使用添加自定义 HTTP 标头HttpRequestMessageProperty。请参考下面的代码。

\n\n
ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();\ntry\n{\n    using (OperationContextScope ocs=new OperationContextScope(client.InnerChannel))\n    {\n        var requestProp = new HttpRequestMessageProperty();\n        requestProp.Headers["myhttpheader"] = "Boom";\n        OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestProp;\n        var result = client.SayHelloAsync();\n        Console.WriteLine(result.Result);\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

结果。
\n在此输入图像描述
\nWebOperationContext 是 OperationContext 的便捷包装器。目前,它还没有在Aspnet Core中实现。
\n https://github.com/dotnet/wcf/issues/2686
\n如果有什么我可以帮忙的,请随时告诉我。

\n