请教一个 C# HttpWebRequest session 问题
public CookieContainer myCookieContainer = null;
private void button4_Click(object sender, EventArgs e)
{
ASCIIEncoding encodingA = new ASCIIEncoding();
//=======DATA==========================================================
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "org=xxx";
postData += "&user=xxxxx";
postData += "&pwd=xxx";
byte[] data = encoding.GetBytes(postData);
//=======POST================================================================================
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("xxx");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
myRequest.CookieContainer = myCookieContainer;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
textBox1.Text = reader.ReadToEnd();
}
private void GetPageData(string URL)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
myRequest.Method = "GET";
myRequest.CookieContainer = myCookieContainer;
HttpWebResponse myResponseA = (HttpWebResponse)myRequest.GetResponse();
StreamReader readerA = new StreamReader(myResponseA.GetResponseStream(), Encoding.Default);
textBox1.Text = readerA.ReadToEnd();
}
按下按钮后返回的HTTP是登录信息,但是执行GetPageData返回的HTTP提示超时。是不是CookieContainer用的不对。