【问题标题】:C# WebRequest using Cookies使用 Cookie 的 C# WebRequest
【发布时间】: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


【解决方案1】:

这样的东西应该可以工作,我正在使用类似的代码来保存登录 cookie:

HttpWebRequest runTest;
//...do login request
//get cookies from response

CookieContainer myContainer = new CookieContainer();
for (int i = 0; i < Response.Cookies.Count; i++)
{
   HttpCookie http_cookie = Request.Cookies[i];
   Cookie cookie = new Cookie(http_cookie.Name, http_cookie.Value, http_cookie.Path);
   myContainer.Add(new Uri(Request.Url.ToString()), cookie);
}

//later:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.url.com/foobar");
request.CookieContainer = myContainer;

【讨论】:

  • 我想我不明白如何实现你的想法。 runTest 请求将在初始登录后完成,我不知道 cookie 的名称。能详细点吗?
  • @Alex:我修改了我对 http cookie 的回答——我的初始响应是我使用 SOAP 服务改编的,您可以在其中发出登录请求,然后从同一个变量中获取 cookie
  • 我不确定我的代码到底适合哪里。我没有“响应”变量,我的应用程序不了解 HttpCookie 是什么。再次感谢您的帮助。
  • Alex:检查您的代码:requestLogin.GetResponse() 是您正在寻找的响应对象。对于 HttpCookie,您必须在您的应用中添加对 System.Web 的引用。
  • 碎玻璃,检查 OP 我更新了一些代码,基本上做了我认为基本相同的事情,但它仍然没有工作。
猜你喜欢
  • 2010-10-13
  • 1970-01-01
  • 1970-01-01
  • 2010-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-07
相关资源
最近更新 更多