c#模拟登录验证码错误的问题,急急急。。。。马上结。。
public static CookieContainer CookieContainers = new CookieContainer();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string username = textBox2.Text.Trim();
string password = textBox3.Text.Trim();
string code = textBox4.Text.Trim();
string data="__VIEWSTATE=dDwtMTg3MTM5OTI5MTs7PhMbjAbINgvvHsvy%2FQdjcw3RMlRn&TextBox1="+username+"&TextBox2="+password+"&TextBox3="+code+"&RadioButtonList1=%D1%A7%C9%FA&Button1=&lbLanguage=";
string loginurl = "http://xk4.suda.edu.cn/";
getresponse(loginurl,data);
}
private void getresponse(string loginurl,string data)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(loginurl);
req.KeepAlive = true;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = "User-AgentMozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
req.Accept = "Accepttext/html, application/xhtml+xml, */*";
req.Timeout = 50000;
req.AllowAutoRedirect = true;
req.CookieContainer = CookieContainers;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] postBytes = encoding.GetBytes(data);
req.ContentLength = postBytes.Length;
Stream st = req.GetRequestStream();
st.Write(postBytes, 0, postBytes.Length);
st.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream resset = res.GetResponseStream();
Encoding myEncoding = Encoding.GetEncoding("gb2312");
StreamReader sr = new StreamReader(resset, myEncoding);
string str = sr.ReadToEnd();
textBox1.Text = str;
webBrowser1.DocumentText = textBox1.Text;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
HttpWebRequest webr = (HttpWebRequest)WebRequest.Create("http://xk4.suda.edu.cn/CheckCode.aspx");
HttpWebResponse res = (HttpWebResponse)webr.GetResponse();
Stream stream=res.GetResponseStream();
pictureBox1.Image = Image.FromStream(stream);
stream.Close();
req.Timeout = -1;
req.AllowAutoRedirect = true;
req.CookieContainer = CookieContainers;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] postBytes = encoding.GetBytes(data);
req.ContentLength = postBytes.Length;
Stream st = req.GetRequestStream();
st.Write(postBytes, 0, postBytes.Length);
st.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream resset = res.GetResponseStream();
Encoding myEncoding = Encoding.GetEncoding("gb2312");
StreamReader sr = new StreamReader(resset, myEncoding);
string str = sr.ReadToEnd();
textBox1.Text = str;
webBrowser1.DocumentText = textBox1.Text;
}