|
楼主 |
发表于 2015-4-9 16:05:00
|
显示全部楼层
登录页面产生的cookie:
验证码页面:
实现代码:
//第一种获取登陆页面cookie用于获取验证码和post业务
//string fristUrl = "http://www.plateno.com/login.html?from=sso";
//HttpItem fristItem = new HttpItem
//{
// URL = fristUrl,
// Referer = fristUrl,
// ResultCookieType = ResultCookieType.CookieCollection,
// ResultType = ResultType.String
//};
//HttpResult fristResult = _HttpHelper.GetHtml(fristItem);
//fristCookieCollection = fristResult.CookieCollection;
//logCookie = fristResult.Cookie;
//第二步Get 携带第一次的cookie请求验证码
if (!string.IsNullOrEmpty(imgUrl))
{
string ImgUrl = imgUrl + "&r=" + DateTime.Now.Ticks.ToString();
string ImgPath = AppDomain.CurrentDomain.BaseDirectory + "/validateImg.jpg";
HttpItem httpImgItem = new HttpItem
{
KeepAlive = true,
URL = ImgUrl,
ResultType = ResultType.Byte,
Referer = ImgUrl,
ResultCookieType = ResultCookieType.CookieCollection
};
HttpResult imgResult = _HttpHelper.GetHtml(httpImgItem);
byte[] bytes = imgResult.ResultByte;
File.WriteAllBytes(ImgPath, bytes);
cookieImgCollection = imgResult.CookieCollection;
cookieImgStr = imgResult.Cookie;
imgCode = DCHelper.GetImgValidateCode(ImgPath, Application.AppOperations.SystemArgs.SysArgs.ImgValidateAccountInfo.Rows[0]["Account"].ToString(), Application.AppOperations.SystemArgs.SysArgs.ImgValidateAccountInfo.Rows[0]["Pwd"].ToString());
}
//第三步登陆
Dictionary<string, string> reqDic = DataRowToDic(row);
reqDic[bDt.Columns[bDt.Columns.Count - 1].ColumnName] = imgCode;
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("fromType", "0");
dic.Add("username", "18802030513");
dic.Add("password", "kpe");
dic.Add("lgCheckcode", imgCode);
// dic.Add("atk", "2c7e59fae0c7416ea435ac3ecb682111");
// dic.Add("j_expiredays", "0");
// dic.Add("passwordType", "0");
//第三步发送POST请求
HttpItem item = new HttpItem
{
Referer = loginUrl,
URL = loginUrl,
Method = "Post",
PostEncoding = Encoding.UTF8,
ContentType = "application/x-www-form-urlencoded",
PostDic = dic,
ResultCookieType = ResultCookieType.CookieCollection,
Cookie = cookieImgStr
};
HttpResult httpResult= _HttpHelper.GetHtml(item);
resul = httpResult.Html;
object pr1 = JsonTools.JsonToObject(resul, new RegResultInfo());
RegResultInfo res1 = (RegResultInfo)pr1;
if (res1.result == "1")
{
CompleteSql = string.Format(CompleteSql, 1, resul, row["id"].ToString());
}
else
CompleteSql = string.Format(CompleteSql, 0, resul, row["id"].ToString());
|
|