【发布时间】:2011-05-08 16:34:53
【问题描述】:
我有一个我一直在开发的 winforms 应用程序,它在消费者帐户上运行多个测试。测试需要一次性登录才能执行。
string paramaters = "authmethod=on&chkRememberMe=on&login-form-type=pwd&password=" + pw.Text + "&userid=" + uid.Text + "&username=" + uid.Text;
string strResponse;
HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create("https://www.url.com/login.form");
requestLogin.Method = "POST";
requestLogin.CookieContainer = cookieJar;
requestLogin.ContentType = "application/x-www-form-urlencoded";
requestLogin.ContentLength = paramaters.Length;
StreamWriter stOut = new StreamWriter(requestLogin.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(paramaters);
stOut.Close();
StreamReader stIn = new StreamReader(requestLogin.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
stIn.Close();
这个脚本适用于登录就好了,问题是当我需要实际运行测试时,我需要将所有结果返回到一个字符串(HTML 结果)中。
private string runTestRequest(Uri url, string parameters)
{
string testResults = string.Empty;
HttpWebRequest runTest = (HttpWebRequest)WebRequest.Create(url);
runTest.CookieContainer = cookieJar;
runTest.Method = "POST";
runTest.ContentType = "application/x-www-form-urlencoded";
StreamWriter stOut = new StreamWriter(runTest.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(parameters);
stOut.Close();
StreamReader stIn = new StreamReader(runTest.GetResponse().GetResponseStream());
testResults = stIn.ReadToEnd();
stIn.Close();
return testResults;
}
但它会尝试登录。我如何将上一个登录请求中的 Cookie 与此 + 许多其他网络请求一起使用?
感谢您的帮助。
编辑:
我将它添加到我的代码中,但它应该和 BrokenGlass 所说的一样,只是有点不同,但仍然不起作用。
foreach (Cookie cookie in responseLogin.Cookies)
{
cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
richTextBox2.Text += cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString();
}
【问题讨论】:
-
您在运行新请求时检查过 cookieJar 中的内容吗?里面有什么吗?
-
我做了一个 MessageBox.Show(cookieJar.Count.ToString());它显示计数为 2
标签: c# winforms cookies webrequest