本帖最后由 zhty2014 于 2014-10-11 14:44 编辑
如题,我先使用HttpWebRequest方式获取数据的,多次获取后出现服务器无法访问,改用HttpHelper,但发现多次GET后发现还是最后一次GET报错。下面是代码,请大神指点。
[C#] 纯文本查看 复制代码 string username = textBox1.Text.Trim();// username= yuansh72
string password = textBox2.Text.Trim(); //password= yuan8050
CookieCollection DLCookies = new CookieCollection();
HttpHelper http = new HttpHelper();
HttpResult result = new HttpResult();
HttpItem item = new HttpItem() //登陆Post
{
URL = "https://learning.sinopec.com/pkmslogin.form",
Method = "POST",
Referer = "https://learning.sinopec.com/",
ContentType = "application/x-www-form-urlencoded",
Postdata = string.Format("action=1200&srcreq=1001&site=sinopec&username=" + username + "&password=" + password + "&login-form-type=pwd"),
ResultCookieType = ResultCookieType.CookieCollection
};
result = http.GetHtml(item);
foreach (Cookie cc in result.CookieCollection) //将cookie设置为浏览的cookie
{
InternetSetCookie(
"http://",
cc.Name.ToString(),
cc.Value.ToString() + ";expires=+ DateTime.Now.AddDays(1).ToString()");
DLCookies.Add(cc);
}
//====================================POST成功
item = new HttpItem()
{
URL = "https://learning.sinopec.com/ilearn/en/cst_admin/jsp/userlogin.jsp",
Encoding = System.Text.Encoding.GetEncoding("utf-8"),
CookieCollection = DLCookies,
ContentType = "application/x-www-form-urlencoded",
Allowautoredirect = true,
ResultCookieType = ResultCookieType.CookieCollection
};
result = http.GetHtml(item);
foreach (Cookie ccc in result.CookieCollection) //将cookie设置为浏览的cookie
{
DLCookies.Add(ccc);
}
////===================================登录成功
item = new HttpItem()
{
URL = "https://learning.sinopec.com/ilearn/en/userhome/jsp/userhome_new.jsp",
Encoding = System.Text.Encoding.GetEncoding("utf-8"),
CookieCollection = DLCookies,
ContentType = "application/x-www-form-urlencoded",
Allowautoredirect = true,
ResultCookieType = ResultCookieType.CookieCollection
};
result = http.GetHtml(item);
////==========================================页面跳转成功
///===========================================
string text = "my_course.jsp?";
string ass = ";";
string ShiPinShu = Between(result.Html, text, ass);
string Shu = ShiPinShu.Substring(0, ShiPinShu.Length - 1);
long s = long.Parse(Shu);
///===========================================以上是取出要打开连接的尾数
item = new HttpItem()
{
URL = "http://learning.sinopec.com/ilearn/en/userhome/jsp/my_course_new.jsp?" + s,
Encoding = System.Text.Encoding.GetEncoding("utf-8"),
CookieCollection = DLCookies,//把Cookie写入请求串中
ContentType = "application/x-www-form-urlencoded",
//Allowautoredirect = true,
ResultCookieType = ResultCookieType.CookieCollection
};
result = http.GetHtml(item);
//////=============================================GET失败
richTextBox1.Text = result.Html; 是不是最后那个URL不对?该怎么写呢,请大神指点
|