好的,我昨天尝试提出了这个问题,但我不确定是否提供了足够的信息,我得到了一个答案,但对我没有起作用。基本上,我正在做的是用户打开这个Windows窗体应用程序并登录。之后,他们在文本框中输入一些文本,然后单击运行。此时,运行函数正在向需要登录的服务器发出Web请求(最初在打开程序后完成的登录)。由于某种原因,在执行第二个请求时仍然无法看到用户已登录,即使添加了cookie到cookie容器中。我不确定我做错了什么,但我会发布我的代码,以便您可以进一步帮助我。
这是用户输入应用程序时执行的登录功能。
private void button1_Click(object sender, EventArgs e)
{
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();
HttpWebResponse responseLogin = (HttpWebResponse)requestLogin.GetResponse();
StreamReader stIn = new StreamReader(responseLogin.GetResponseStream());
strResponse = stIn.ReadToEnd();
stIn.Close();
//Add cookies to CookieJar (Cookie Container)
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();
}
if (strResponse.Contains("Log On Successful") || strResponse.Contains("already has a webseal session"))
{
foreach (Control cont in this.Controls)
{
cont.Visible = true;
}
loginPanel.SendToBack();
loginPanel.Visible = false;
}
else
{
MessageBox.Show("Login failed.");
}
}
当用户点击“运行”按钮以启动对消费者账户的测试时,将运行此函数。
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容器对象
public CookieContainer cookieJar = new CookieContainer();
P.S.: 网络请求的域名不同。第一个是abc.com,第二个是123.com。唯一的问题是第一个域名(即登录)是内部Web应用程序(如123.com)的全局登录,那么我该如何使用来自第一个域名的登录会话与第二个域名进行通信?
请协助我找出错误所在。