[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WindowsFormsRs;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using DotNet.Utilities;
namespace _126注册
{
public partial class Form1 : Form
{
#region 全局子段
string KnSkyCookie = "";//全局cookie
string envalue;//网页envalue
string sid;//jsessionid=
#endregion
public Form1()
{
InitializeComponent();
}
#region 第一次登录取值
string webhttp()
{
txtuser.Text = RanUser(true);
string resolt = KnSkyHttp.KnSkyGet("http://reg.email.163.com/unireg/call.do?cmd=register.entrance&from=126mail", KnSkyCookie,
ref KnSkyCookie, null, " [url]http://126.com/[/url]", 5000, true);
envalue = QuMiddle(resolt, "envalue : \"", "\",");
sid = QuMiddle(resolt, "sid : \"", "\",");
return resolt;
}
#endregion
#region 验证码
void code()
{
webhttp();
mailcode();
}
void mailcode()
{
string cookie = KnSkyCookie;
//HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://reg.email.163.com/unireg/call.do?cmd=register.verifyCode&v=common/verifycode/vc_en&env=" + pgv_pvid(envalue).Trim() + "&t=" + NOWDATE().Trim());
//req.Proxy = null;
//HttpWebResponse res = (HttpWebResponse)req.GetResponse();
//using (Stream stream = res.GetResponseStream())
//{
// picCode.Image = Image.FromStream(stream);
//}
picCode.Image = Image.FromStream(KnSkyHttp.KnSkyPicture("http://reg.email.163.com/unireg/call.do?cmd=register.verifyCode&v=common/verifycode/vc_en&env=" + pgv_pvid(envalue).Trim() + "&t=" + NOWDATE().Trim(), cookie, ref cookie, "http://reg.email.163.com/unireg/call.do?cmd=register.entrance&from=126mail"));
}
#endregion
#region 正则表达式
public static string QuMiddle(string str, string str1, string str2)
{
Regex regex = new Regex(string.Format("(?<={0}).*?(?={1})", str1, str2), RegexOptions.Compiled);
Match match = regex.Match(str);
if (match.Success)
{
return match.Value;
}
return "未设置正确";
}
#endregion
#region js方法
public static object GetRSAPassword(string jsFilePath, string funcName, params object[] paramers)
{
using (StreamReader reader = new StreamReader(jsFilePath))
{
string sScript = reader.ReadToEnd();
ScriptEngine se = new ScriptEngine(ScriptLanguage.JavaScript);
object obj = se.Run(funcName, paramers, sScript);
return obj;
}
}
#endregion
#region 取两个JS值
string pgv_pvid(string ENV)
{
object str = GetRSAPassword(Application.StartupPath + "\\JScript1.js", "__$0", ENV);
return str.ToString();
}
string NOWDATE()
{
object str = GetRSAPassword(Application.StartupPath + "\\JScript1.js", "I", "");
return str.ToString();
}
#endregion
#region 注册按钮
private void btnlogin_Click(object sender, EventArgs e)
{
string user = txtuser.Text.Trim().ToLower();
string code = txtcode.Text.Trim();
textBox3.Text = KnSkyHttp.KnSkyPost("https://ssl.mail.163.com/regall/unireg/call.do;jsessionid=" + sid + "?cmd=register.start&reforward=common/reform&targetCmd=register.ctrlTop",
"name=" + user + "&flow=main&uid=" + user + "@126.com&password=jhty123&confirmPassword=jhty123&mobile=&vcode=" + code + "&from=126mail",
KnSkyCookie, ref KnSkyCookie, "", "http://reg.email.163.com/unireg/call.do?cmd=register.entrance&from=126mail", 5000, true);
#region 苏飞的方法,试了没用返回401
// HttpHelper http = new HttpHelper();
// HttpItem item = new HttpItem()
// {
// URL = "https://ssl.mail.163.com/regall/unireg/call.do;jsessionid=" + sid + "?cmd=register.start&reforward=common/reform&targetCmd=register.ctrlTop",//URL 必需项
// Encoding = "utf-8",//编码格式(utf-8,gb2312,gbk) 可选项 默认类会自动识别
// Method = "POST",//URL 可选项 默认为Get
// Timeout = 100000,//连接超时时间 可选项默认为100000
// ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000
// IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
// Cookie = KnSkyCookie,//字符串Cookie 可选项
// UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统 可选项有默认值
// Accept = "text/html, application/xhtml+xml, */*",// 可选项有默认值
// ContentType = "text/html",//返回类型 可选项有默认值
// Referer = "http://reg.email.163.com/unireg/call.do?cmd=register.entrance&from=126mail",//来源URL 可选项
// Allowautoredirect = true,//是否根据301跳转 可选项
// //CerPath = "d:\\123.cer",//证书绝对路径 可选项不需要证书时可以不写这个参数
// //Connectionlimit = 1024,//最大连接数 可选项 默认为1024
// Postdata = "name=" + user + "&flow=main&uid=" + user + "@126.com&password=jhty123&confirmPassword=jhty123&mobile=&vcode=" + code + "&from=126mail",//Post数据 可选项GET时不需要写
// //PostDataType=PostDataType.FilePath,//默认为传入String类型,也可以设置PostDataType.Byte传入Byte类型数据
// //ProxyIp = "192.168.1.105",//代理服务器ID 可选项 不需要代理 时可以不设置这三个参数
// //ProxyPwd = "123456",//代理服务器密码 可选项
// //ProxyUserName = "administrator",//代理服务器账户名 可选项
// ResultType = ResultType.Byte,//返回数据类型,是Byte还是String
// };
// //得到HTML代码
// HttpResult result = http.GetHtml(item);
// //取出返回的Cookie
// string cookie = result.Cookie;
// //得到返回的Byte数组
// byte[] bytelist = result.ResultByte;
// //返回的Html内容
//textBox3.Text = result.Html;
#endregion
}
#endregion
#region 执行验证码方法
private void btnCode_Click(object sender, EventArgs e)
{
code();
}
#endregion
#region 取随机帐号
public string RanUser(bool Sleep)
{
lock (this)
{
if (Sleep) System.Threading.Thread.Sleep(10);
string[] pinyins = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
Random r = new Random();
int l2 = r.Next(10000);
Random ran = new Random();
string username = "";
for (int i = 0; i < 4; i++)
{
username += pinyins[ran.Next(0, pinyins.Length)];
}
return username + l2;
}
}
#endregion
private void picCode_Click(object sender, EventArgs e)
{
mailcode();
}
}
}