C#post模拟登陆教务系统失败
本文介绍了C#post模拟登陆教务系统失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
我在进行post登陆教务系统但是总是登陆不进去,返回给我的页面仍然是登陆页面,学校教务系统没有cookie返回只有url间的sessionid。麻烦各位大侠帮我看一下是什么地方的问题。
1、获取sessionid的代码
private string GetUrl()
{
string urlGetSession = "http://xk.zucc.edu.cn";
HttpWebRequest reqGetSession =(HttpWebRequest)WebRequest.Create(urlGetSession);
reqGetSession.Method = "GET";
reqGetSession.KeepAlive = true;
//禁止网页进行重定向
reqGetSession.AllowAutoRedirect = false;
reqGetSession.UserAgent = "Mozilla / 5.0(Windows NT 10.0; WOW64; rv: 51.0) Gecko / 20100101 Firefox / 51.0";
//创建接受网页响应的对象
HttpWebResponse resGetSession = (HttpWebResponse)reqGetSession.GetResponse();
Stream streamGetSession = resGetSession.GetResponseStream();
StreamReader srGetSession = new StreamReader(streamGetSession);
string strGetSession = srGetSession.ReadToEnd();
string[] strGetSessions = strGetSession.Split(new string[] { "/(",")/"},StringSplitOptions.None);
return strGetSessions[1];
}
2、获取验证码的代码
private void GetValidateImage(string urlParma)
{
cookies = new CookieContainer();
string url = "http://xk.zucc.edu.cn/(" + urlParma + ")/CheckCode.aspx";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Method = "GET";
request.UserAgent = "Mozilla / 5.0(Windows NT 10.0; WOW64; rv: 51.0) Gecko / 20100101 Firefox / 51.0";
//request.CookieContainer = new CookieContainer(); //暂存到新的实例中
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
MemoryStream ms = null;
using (var stream = response.GetResponseStream())
{
byte[] buffer = new byte[response.ContentLength];
int offset = 0;
int actuallyRead = 0;
do
{
actuallyRead = stream.Read(buffer, offset, buffer.Length - offset);
offset += actuallyRead;
}
while (actuallyRead > 0);
ms = new MemoryStream(buffer);
}
response.Close();
cookies = request.CookieContainer;//保存cookies
//strCookies = request.CookieContainer.GetCookieHeader(request.RequestUri);//把cookies转换成字符串
Bitmap bitmap = new Bitmap((Stream)ms);
imgValidate.Image = bitmap;
}
3、post登陆代码
private string Login2(string urlParma)
{
Encoding enc = Encoding.GetEncoding("gb2312");
string postData = string.Format("__VIEWSTATE=dDwtNTE2MjI4MTQ7Oz7eLwpTkU0eZiReI563hfS1TF2rsQ%3D%3D&txtUserName={0}&Textbox1=&TextBox2={1}&txtSecretCode={2}&RadioButtonList1=% D1 % A7 % C9 % FA & Button1 = &lbLanguage = &hidPdrs = &hidsc = ", txtUserName.Text, txtPassword.Text, txtValidate.Text);
//把要提交的数据转换成字节数组
byte[] pb = enc.GetBytes(postData);
string url = "http://xk.zucc.edu.cn/(" + urlParma + ")/default2.aspx";
//新建一个请求
HttpWebRequest myreq = (HttpWebRequest)WebRequest.Create(url);
//指定请求的方法
myreq.Method = "POST";
myreq.ContentType = "application/x-www-form-urlencoded";
myreq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
//这里请求文本的长度就是我们字节数组的长度
myreq.ContentLength = pb.Length;
//获取提交流
Stream str = myreq.GetRequestStream();
str.Write(pb, 0, pb.Length);
str.Close();
WebResponse myresp = myreq.GetResponse();//获取返回的请求
Stream revstr = myresp.GetResponseStream(); //获取流
StreamReader readstr = new StreamReader(revstr,enc);//准备读取流
Char[] readc = new Char[256];
//每次读256个「字符」
int count = readstr.Read(readc, 0, 256);
string restr = "";
while (count > 0)
{
for(int i = 0;i < count;i++)
{
restr += readc[i].ToString();
}
count = readstr.Read(readc,0,256);
}
readstr.Close();
myresp.Close();
restr = restr.Replace("\t","");
return restr;
}
winform的东西
解决方案
采用苏飞站长的HttpHelper类库重新分析了一遍,就可以顺利Post进去了
这篇关于C#post模拟登陆教务系统失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文