【发布时间】:2011-02-27 16:41:27
【问题描述】:
我正在创建一个从网页检索数据的应用程序。该页面受密码保护,并在用户登录时创建 cookie。
为了检索数据,应用程序首先必须登录:使用用户名和密码进行 Web 请求并存储 cookie。然后当 cookie 被存储时,它必须被添加到所有请求的头部中。
这是发出请求并检索内容的方法:
public void getAsyncDailyPDPContextActivationDeactivation()
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(dailyPDPContextActivationDeactivation);
IAsyncResult asyncResult = httpWebRequest.BeginGetResponse(null, null);
asyncResult.AsyncWaitHandle.WaitOne();
using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(asyncResult))
using (StreamReader responseStreamReader = new StreamReader(httpWebResponse.GetResponseStream()))
{
string responseText = responseStreamReader.ReadToEnd();
}
}
有谁知道如何修改此方法以便将 cookie 添加到标头中?
如果有人建议一种方法来存储响应中的 cookie,我也将不胜感激(当应用程序发出请求 http:xxx.xxx.xxx/login?username=xxx&password=xxx 时,cookie 被创建并且必须被存储用于将来的请求)。
【问题讨论】:
标签: .net httpwebrequest cookies