HttpWebRequest和HttpWebResponse:的登录请求,连续查询维护状态 [英] HttpWebRequest and HttpWebResponse : maintaining state of logged in request for consecutive queries

查看:184
本文介绍了HttpWebRequest和HttpWebResponse:的登录请求,连续查询维护状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个HttpWebRequests和HttpWebResponses链接在一起,还用的CookieContainer。

I have a few HttpWebRequests and HttpWebResponses chained together, also using CookieContainer.

在code模拟用户通过将三种不同的我同意该设置Cookie信息页面,用户名和密码在第四登录,做在第五一个POST(搜索)返回响应,字符串。

The code simulates a user going through three different 'I agree' pages which set the cookie info, logging in with username and password on a fourth, and doing a POST (search) on the fifth returning the response as a string.

有没有办法为'登录'以避免通过这些步骤,每一个任何用户执行搜索的时间去我能保持HttpWebRequest对象?

Is there a way I can maintain the HttpWebRequest object as 'logged in' to avoid going through those steps each time any user performs a search?

我可以将其设置为静态的,如果它的空或缺少的cookie信息就可以完成所有步骤,否则运行只是做用户需要的职位?什么是一个很好的模式?

Can I set it up as static, and if its null or lacking cookie info it can run through all steps otherwise just do the post the user requires? What is a good pattern for this?.

推荐答案

如果你正在登录服务器中使用基于Cookie的身份验证,那么你必须创建您存储的身份验证cookie一个System.Net.CookieContainer。这是很简单的:

If the server you are logging in to uses cookie based authentication then you must create a System.Net.CookieContainer where you store the authentication cookie. This is quite simple:

CookieContainer container = new CookieContainer();

// Create web request
request.CookieContainer = container;

// Create next web request
nextRequest.CookieContainer = container;

// And so on
...

只需使用你所有的HttpWebRequest对象的CookieContainer对象,并将其保存在内存中,供以后使用。

Just reuse the CookieContainer object for all your HttpWebRequest objects, and keep it in memory for later use.

本的CookieContainer是序列化的,所以你可以坚持在磁盘上,如果你需要。这将让你保持你的cookie(S),甚至当你的用户重新启动应用程序。

The CookieContainer is Serializable, so you can persist it on disk if you need to. That will allow you to keep your cookie(s) even when your user restarts your application.

另外,如果页面没有在URL中使用Cookie,但存储会话ID,而不是,你需要不断传递下去会话ID在页面中你wisit的URL。只要将其追加到URL的,它应该工作。 : - )

Alternatively, if the page doesn't use cookies but stores the session id in the url instead, you need to keep pass along the session id in the url of the pages you wisit. Just append it to the url's and it should work. :-)

这篇关于HttpWebRequest和HttpWebResponse:的登录请求,连续查询维护状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆