本文介绍了C#中使用的WebRequest饼干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms应用程序我一直在运行对消费者帐户的多个测试。的测试,以执行需要一次登录

 字符串PARAMATERS =authmethod = ON&安培; chkRememberMe = ON&安培;登录表单型= PWD和放大器;密码=+ pw.Text +&放大器;用户id =+ uid.Text +&放大器;用户名=+ uid.Text;
        串strResponse;
        HttpWebRequest的requestLogin =(HttpWebRequest的)WebRequest.Create(https://www.url.com/login.form);
        requestLogin.Method =POST;
        requestLogin.CookieContainer = cookieJar;
        requestLogin.ContentType =应用/的X WWW的形式urlen codeD;        requestLogin.ContentLength = paramaters.Length;
        粗壮的StreamWriter =新的StreamWriter(requestLogin.GetRequestStream(),System.Text.Encoding.ASCII);
        stOut.Write(PARAMATERS);
        stOut.Close();        StreamReader的STIN =新的StreamReader(requestLogin.GetResponse()GetResponseStream());
        strResponse = stIn.ReadToEnd();
        stIn.Close();

本脚本适用于登录就好了,问题是,当我需要实际运行,我需要所有的结果返回到一个字符串(HTML结果)测试。

 私人字符串runTestRequest(URI链接,字符串参数)
    {
        字符串testResults =的String.Empty;
        HttpWebRequest的的runTest =(HttpWebRequest的)WebRequest.Create(URL);
        runTest.CookieContainer = cookieJar;
        runTest.Method =POST;
        runTest.ContentType =应用/的X WWW的形式urlen codeD;
        粗壮的StreamWriter =新的StreamWriter(runTest.GetRequestStream(),System.Text.Encoding.ASCII);
        stOut.Write(参数);
        stOut.Close();
        StreamReader的STIN =新的StreamReader(runTest.GetResponse()GetResponseStream());
        testResults = stIn.ReadToEnd();
        stIn.Close();
        返回testResults;
    }

但它去,并试图登录。我该如何使用Cookie从本+许多其他的Web请求previous登录请求?

感谢您的帮助。

编辑:

我说这我的code还应该做同样的事情BrokenGlass是除了一点点不同的说法,但仍然无法正常工作。

 的foreach(在responseLogin.Cookies饼干饼干)
        {
            cookieJar.Add(新的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();
        }


解决方案

这样的事情应该工作,我使用类似code保存登录cookie:

  HttpWebRequest的runTest方法;
//...do登录请求
//得到响应cookie的CookieContainer myContainer中=新的CookieContainer();
的for(int i = 0; I< Response.Cookies.Count;我++)
{
   的HttpCookie HTTP_COOKIE = Request.Cookies时[I]
   饼干饼干=新的Cookie(http_cookie.Name,http_cookie.Value,http_cookie.Path);
   myContainer.Add(新的URI(Request.Url.ToString()),饼干);
}//后来:
HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(http://www.url.com/foobar);
request.CookieContainer = myContainer中;

I have a winforms application i have been working on that runs multiple tests on consumer accounts. The tests require a one time login in order to execute.

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();

This script works for the login just fine, the problem is when i need to actually run the tests i need to return all the results into a string (HTML results).

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;
    }

But it goes and tries to login. How can i use the Cookie from the previous login request with this + many other web requests?

Thanks for the help.

EDIT:

I added this to my code yet which should do the same thing as BrokenGlass is saying except just a little different but still doesn't work.

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();
        }
解决方案

Something like this should work, I am using similar code to save a login 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;

这篇关于C#中使用的WebRequest饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:23