向Web服务端口客户端添加cookie
我在我的应用程序中使用一个 Web 服务,需要设置特定的 cookie 才能访问它的方法。
我使用的是使用 wsdl.exe 工具创建的该服务的生成包装类。 使用该方法一切正常。
// this is the instance of object generated with wsdl.exe
WSWrapper service = new WSWrapper();
// set cookie
service.CookieContainer = new CookieContainer();
Cookie cookie = new Cookie(name, value, path, domain);
service.CookieContainer.Add(cookie);
// run method requiring cookie to be set
service.Test();
现在我想使用服务引用而不是预先生成的类来做类似的事情。 我添加了 Web 引用,但生成的服务引用端口客户端中似乎没有 CookieContainer
(或类似的内容)。
有谁知道如何向该客户端添加 cookie?
I'm using a web service in my app that requires a specific cookie to be set in order access it's methods.
I was using a generated wrapper class for that service that was created using wsdl.exe
tool. Everything is working ok using that method.
// this is the instance of object generated with wsdl.exe
WSWrapper service = new WSWrapper();
// set cookie
service.CookieContainer = new CookieContainer();
Cookie cookie = new Cookie(name, value, path, domain);
service.CookieContainer.Add(cookie);
// run method requiring cookie to be set
service.Test();
Now I wanted to do something similar using Service Reference instead of pre-generated class. I added web reference but there seems to be no CookieContainer
(or anything similar) in service reference port client that was generated.
Does anyone knows how to add a cookie to that client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不过,
svcutil.exe
生成的代理有一些优点。现在可以通过 app.config 控制客户端保存并返回服务器返回的 cookie(过去只需添加 CookieContainer 即可完成)。 将
allowCookies="true
" 添加到basicHttpBinding
或basicHttpsBinding
作为绑定的属性。或者使用 WCF 配置编辑器执行相同的操作。
There are advantages to the
svcutil.exe
generated proxies though.Having the client save and return the cookies returned by the server (as used to be done by simply adding a
CookieContainer
) can now be controlled through app.config. AddallowCookies="true
" to thebasicHttpBinding
orbasicHttpsBinding
as an attribute to the binding.Alternatively use the WCF configuration editor to do the same.
我解决了这个问题。 我没有创建服务引用,而是添加了 Web 引用,并且生成的客户端具有
wsdl.exe
预生成类的所有属性。I solved the problem. Instead of creating Service Reference I added Web Reference and the generated client had all the properties of the
wsdl.exe
pre-generated class.