RT哈,废话不多说,直接上抓包数据分析及代码:
第一步:
获取注册页面源代码,后续取得验证码的时候需要计算envalue的值,代码如下:
[C#] 纯文本查看 复制代码 //打开页面,获取envalue
string url = "http://reg.email.163.com/unireg/call.do?cmd=register.entrance&from=126mail";
string host = "reg.email.163.com";
string referer = "http://reg.email.163.com/unireg/call.do?cmd=register.entrance&from=126mail";
HttpHelper http = new HttpHelper();
string timemp = time.Substring(0, time.IndexOf("."));
HttpItem item = new HttpItem()
{
URL = url,
Host = host,
ContentType = ContentType
};
HttpResult result = http.GetHtml(item);
cookieString = result.Cookie;
string html = result.Html;
string envalue = Regex.Match(html, "(?is)(?<=envalue : \")[^\"]+(?=\")").Value;
这时cookieString已经保存了本次访问获取的cookie值,我们需要提取出“JSESSIONID”和“ser_adapter”备用,见代码:[C#] 纯文本查看 复制代码 //从cookie中提取sid
sid = HttpCookieHelper.GetCookieValue("JSESSIONID", cookieString);
sd = HttpCookieHelper.GetCookieValue("ser_adapter", cookieString);
第二步获取验证码:
这一步比较重要,需要通过第一步获取的envalue调用JavaScript获得计算后的值,js稍后我会提供下载,上代码:
[C#] 纯文本查看 复制代码 public static object GetEnValue(string jsFilePath, string funcName, params object[] paramers)
{
StreamReader reader = new StreamReader(jsFilePath);
string sScript = reader.ReadToEnd();
ScriptEngine se = new ScriptEngine(ScriptLanguage.JavaScript);
object obj = se.Run(funcName, paramers, sScript);
return obj;
}
获取到env的之后请求验证码:
[C#] 纯文本查看 复制代码 string env = GetEnValue("env.js", "env", envalue).ToString();
url = "http://reg.email.163.com/unireg/call.do?cmd=register.verifyCode&v=common/verifycode/vc_en&env=" + env + "&t=" + timemp;
item = new HttpItem()
{
URL = url,
Host = host,
Cookie = cookieString,
Referer = referer,
ContentType = ContentType,
ResultType = ResultType.Byte
};
result = http.GetHtml(item);
pictureBox1.Image = byteArrayToImage(result.ResultByte); 到这里我们已经能正常获取验证码了,如果失败的同学,检查下cookie时候正常传递
第三步,网易注册是https协议的,我们现在要获取ssl提交时需要带的cookie,上代码
[C#] 纯文本查看 复制代码 //开始获取ssl的cookie
url = string.Format("https://ssl.mail.163.com/regall/unireg/prepare.jsp?sid={0}&sd={1}",sid,sd);
item = new HttpItem()
{
URL = url,
Host = "ssl.mail.163.com",
Referer = referer,
ContentType = ContentType
};
result = http.GetHtml(item);
sslCookieString = result.Cookie;
注意这里的参数 sid=第一步我们获取的cookie中“JSESSIONID”的值,sd为cookie中“ser_adapter”的值
到这里我们已经获取到了ssl需要的cookie了
最后一步,正式提交注册,上代码:
[C#] 纯文本查看 复制代码 string username = this.username.Text.Trim();
string password = this.password.Text.Trim();
string code = this.code.Text.Trim();
string url = string.Format("https://ssl.mail.163.com/regall/unireg/call.do;jsessionid={0}?cmd=register.start",sid);
string host = "ssl.mail.163.com";
string data = string.Format("name={0}&flow=main&uid={1}%40126.com&password={2}&confirmPassword={3}&mobile=&vcode={4}&from=126mail", username, username,password,password,code);
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = url,
Host = host,
Postdata = data,
Method = "POST",
ContentType = ContentType,
Cookie = sslCookieString
};
HttpResult result = http.GetHtml(item);
注意这里的cookie,传递的是第三步的cookie哦!!!
如果这时候返回值不是{"code":401,"desc":"PARAMETER ERROR","msg":"VCODE_NOT_MATCH"} 的话,就表示注册成功了哈,如果返回值是这个,检查下cookie是否传递错误,比较重要的一点要注意下ContentType的值,必须自定义设置为“application/x-www-form-urlencoded”,不然会一直提示刚才那个错误,切记!!!
附件上传一个JavaScript的工具
|