【问题标题】:C# unable to assign cookiecontainer to asmx web service clientC# 无法将 cookiecontainer 分配给 asmx Web 服务客户端
【发布时间】:2013-06-24 03:56:22
【问题描述】:

我正在尝试使用 Visual Studio 2012 Express 连接到 C# 中的 asmx Web 服务。

文档说要为 Web 服务分配一个 cookie 容器,如下所示:

using System.Web.Services.Protocols;    /// using appropriate references
using System.Net;
using System.Net.Http;

//   Create an instance of the Web Service and assign in
//   a cookiecontainer to preserve the validated session

ServiceRef.WSClient wsClient = new ServiceRef.WSClient();

CookieContainer cookieJar = new CookieContainer();
wsClient.CookieContainer = cookieJar;

但是,这会导致错误:

'WebServiceTest.ServiceRef.WSClient' does not contain a definition 
for 'CookieContainer' and no extension method 'CookieContainer' 
accepting a first argument of type 'WebServiceTest.ServiceRef.WSClient' 
could be found (are you missing a using directive or an assembly 
reference?)

我尝试将“allowCookies”添加到 app.config。这似乎不起作用;调用需要登录(设置 cookie)的 ws 方法失败。失败意味着我收到一条关于他们返回的 xml 问题的消息(他们可能返回了一些非 xml 错误)。

我对 C#、基于 SOAP 的 Web 服务和 Visual Studio 完全陌生,但我见过大量使用与我的代码完全相同的代码示例。例如:

http://msdn.microsoft.com/en-us/library/system.web.services.protocols.httpwebclientprotocol.cookiecontainer.aspx

http://megakemp.com/2009/02/06/managing-shared-cookies-in-wcf/

【问题讨论】:

  • 您是如何添加服务引用的? WSClient 对象有你的 WebService 方法吗?
  • 从主菜单,“项目”,“添加服务参考”,然后输入指向.asmx页面的url。是的,我可以调用webservice提供的登录方法和ping方法。该服务提供的所有其他方法都需要登录。
  • allowCookies 应该使用您在之前的响应中收到的 cookie 自动设置 cookie。这是您想要的还是您使用预定义的值手动设置它们?我忘了在我的回答中提到它。
  • 还有,asmx服务在你的控制之下吗?
  • 不,我只是一个消费者。

标签: c# web-services cookies soap


【解决方案1】:

您使用的WCF service reference 客户端似乎不包含CookieContainer 属性。

The post that you referenced 实际上包含处理WCF service reference 客户端上的cookie 的方法,最简单的是allowCookies 配置属性,它会自动传递先前响应中收到的cookie。

如果您想使用旧类型的客户端(Web Reference,具有CookieContainer 属性),您可以关注this article

【讨论】:

  • 我曾尝试过“allowCookies”,但由于我的 app.config 中已有另一个绑定,因此它不起作用。我删除了“名称”和“配置”绑定选项,它现在似乎可以工作了。
猜你喜欢
  • 1970-01-01
  • 2014-07-04
  • 1970-01-01
  • 1970-01-01
  • 2016-01-07
  • 2014-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多