|
本帖最后由 moodxiang 于 2017-10-25 13:24 编辑
出现的问题:
第一次获取验证码图片和验证验证码是否正确都能正确返回,但第一次验证验证码是否正确时缺少Cookie:“route”
第二次获取验证码图片和验证验证码是否正确就会出现服务器内部错误,且验证验证码是否正确时缺少Cookie:“JSESSIONID”
请高手给指点下
1、获取验证码图片代码:
/// <summary>
/// 获取验证码图片
/// </summary>
/// <returns>验证码图片</returns>
public static Image getVerificationCodeImg() {
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem() {
//URL = "http://passport.ec.chng.com.cn/samhnec/verificationCode.do?" + GetTimestamp(),
URL = "http://passport.ec.chng.com.cn/samhnec/verificationCode.do",
Method = "get",
CookieCollection = mCookies,
KeepAlive = true,
Host = "passport.ec.chng.com.cn",
Accept = "image/webp,image/apng,image/*,*/*;q=0.8",
Referer = "http://passport.ec.chng.com.cn/samhnec/process.do?action=authuser",
UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
ResultCookieType = ResultCookieType.CookieCollection,
ResultType = ResultType.Byte
};
item.Header.Add("Cache-Control:no-cache");
item.Header.Add("Origin:http://passport.ec.chng.com.cn");
item.Header.Add("ragma:no-cache");
item.Header.Add("Accept-Language:zh-CN,zh;q=0.8");
item.Header.Add("Accept-Encoding:gzip, deflate");
item.Header.Add("Cache-Control:no-cache");
item.Header.Add("Origin:http://passport.ec.chng.com.cn");
item.Header.Add("ragma:no-cache");
item.Header.Add("X-Requested-With:XMLHttpRequest");
// 获取数据
HttpResult result = http.GetHtml(item);
if (result.CookieCollection != null && result.CookieCollection.Count > 0) {
mCookies.Add(result.CookieCollection);
}
if (result.StatusCode == HttpStatusCode.OK) {
return byteArrayToImage(result.ResultByte);
} else {
return null;
}
}
抓包:
GET /samhnec/verificationCode.do HTTP/1.1
Cache-Control: no-cache,no-cache
Origin: http://passport.ec.chng.com.cn,http://passport.ec.chng.com.cn
Pragma: no-cache,no-cache
Accept-Language: zh-CN,zh;q=0.8
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Content-Type: text/html
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Referer: http://passport.ec.chng.com.cn/samhnec/process.do?action=authuser
Host: passport.ec.chng.com.cn
Connection: Keep-Alive
HTTP/1.1 200 OK
Date: Wed, 25 Oct 2017 04:33:03 GMT
Content-Type: image/jpeg;charset=UTF-8
Connection: keep-alive
Pragma: No-cache
Cache-Control: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Language: zh-CN
Set-Cookie: SERVERID=260084a345d85ee7357c9e8fce2cdd9e|1508905983|1508905983ath=/
Set-Cookie: route=47fe9dfb656fc2cb27e6223747033dbb
Set-Cookie: JSESSIONID=29C30E7321D1BDCC8D04CFDF4BBD7889; Path=/samhnec/; HttpOnly
Content-Length: 2345
2、验证验证码是否正确代码:
/// <summary>
/// 检查验证码正确性
/// </summary>
/// <param name="verificationCode">验证码</param>
/// <returns>正确返回true,错误返回false</returns>
public static bool validateCode(string verificationCode) {
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem() {
URL = "http://passport.ec.chng.com.cn/samhnec/validateCode.do",
Method = "post",
CookieCollection = mCookies,
Postdata = "validateCode=" + verificationCode,
Accept = "text/plain, */*; q=0.01",
Referer = "http://passport.ec.chng.com.cn/samhnec/process.do?action=authuser",
Encoding = Encoding.UTF8,
KeepAlive = true,
Host = "passport.ec.chng.com.cn",
ContentType = "application/x-www-form-urlencoded; charset=UTF-8",
UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
ResultCookieType = ResultCookieType.CookieCollection,
ResultType = ResultType.String
};
item.Header.Add("Accept-Encoding:gzip, deflate");
item.Header.Add("Accept-Language:zh-CN,zh;q=0.8");
item.Header.Add("Cache-Control:no-cache");
item.Header.Add("Origin:http://passport.ec.chng.com.cn");
item.Header.Add("ragma:no-cache");
item.Header.Add("X-Requested-With:XMLHttpRequest");
// 获取数据
HttpResult result = http.GetHtml(item);
if (result.CookieCollection != null && result.CookieCollection.Count > 0) {
mCookies.Add(result.CookieCollection);
}
if (result.StatusCode == HttpStatusCode.OK) {
if (result.Html.Equals("fail")) {
return false;
} else {
return true;
}
} else {
System.Windows.Forms.MessageBox.Show("Test");
return false;
}
}
抓包:
POST /samhnec/validateCode.do HTTP/1.1
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8
Cache-Control: no-cache
Origin: http://passport.ec.chng.com.cn
Pragma: no-cache
X-Requested-With: XMLHttpRequest
Accept: text/plain, */*; q=0.01
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Referer: http://passport.ec.chng.com.cn/samhnec/process.do?action=authuser
Host: passport.ec.chng.com.cn
Cookie: JSESSIONID=29C30E7321D1BDCC8D04CFDF4BBD7889; SERVERID=260084a345d85ee7357c9e8fce2cdd9e|1508905983|1508905983
Content-Length: 17
validateCode=m2u9
HTTP/1.1 200 OK
Date: Wed, 25 Oct 2017 04:33:10 GMT
Content-Type: text/plain;charset=ISO-8859-1
Connection: keep-alive
Set-Cookie: route=47fe9dfb656fc2cb27e6223747033dbb
Set-Cookie: SERVERID=260084a345d85ee7357c9e8fce2cdd9e|1508905990|1508905983ath=/
Content-Length: 7
success
|
|