C#无法将cookiecontainer分配给asmx Web服务客户端

时间:2013-06-23 18:31:33

标签: c# web-services cookies soap

我正在尝试使用visual studio 2012 express连接到C#中的asmx Web服务。

文档说要将cookie容器分配给Web服务,如下所示:

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。这似乎不起作用;调用需要登录的ws方法(设置了cookie)失败。失败意味着我收到一条关于他们返回的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/

1 个答案:

答案 0 :(得分:2)

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

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

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