通过winform+模拟登录实现快速一键登录到人才招聘网站
之前为了便于人事部门招聘登录网站更简洁高效,免去每天频繁输网址、用户名、密码等相关登录信息,特基于winform+HttpWebRequest实现模拟请求登录,最终达到一键登录到招聘网站后台的效果。
要实现一键登录到各大人才招聘网站就必需先了解网站的登录步骤即原理,然后通过代码一步步模拟实现即可。
通过我对前程无忧、中国人才热线、中华英才网 三个网站的登录研究,找出基本相同的以下几个步聚:
1.请求登录主页面,然后获取隐藏域的字段信息;
2.构建登录请求数据,有的网站是form表单请求,有的网站是:json请求
3.请求后获取到登录的Cookie,然后通过设置IE的Cookie,最后打开IE浏览器及指定的管理后台地址即可。
除了上述三大步骤以外,其实还涉及一个重要的知识点,那就是:C#调用javascript脚本并获得执行的结果,因为各网站中有用到js生成的加密数据,而这些js加密方法我们无法直接联想到对应的c#方法,故必需采用js方法获取加密数据,这个在本文后面我会讲解几种方法。
上面已经分析了一键登录到人才招聘网站的原理,下面就分别贴出:前程无忧、中国人才热线、 这二个网站的登录实现代码,并作简要说明,以便大家学习与参考:
(PS:注意实现一键登录只是为了便于人事提高网站的使用效率,请不要利用我的代码作非法的事情,否则后果自负,同时也不排除后续这些网站会改变登录方式,那么这些登录就都会失效的)
登录中国人才热线:HttpLoginCjolHelper
public class HttpLoginCjolHelper
{
private static readonly CodeDomProvider _provider = new Microsoft.JScript.JScriptCodeProvider();
private CookieContainer cjolCookies = new CookieContainer(); public string Login(string loginName, string loginPassword)
{ string validateCode, codekey; validateCode = GetValidateCode(out codekey); if (string.IsNullOrEmpty(validateCode)) return null; var rs = HttpPost("http://newrms.cjol.com/Account/HrLogin", new Dictionary<string, object> {
{ "r_u_name", loginName }, { "r_p_word", loginPassword },
{ "r_v_code", validateCode },{ "r_v_codekey", codekey }
}); string newUrl = rs.Headers["Location"].ToString();
if (newUrl.IndexOf("/Default") < 0 && newUrl.IndexOf("message") > 0)
{
string pattren = "(?<=message=).+$";
var regx = new System.Text.RegularExpressions.Regex(pattren);
string errMsg = regx.Match(newUrl).Value;
return Uri.UnescapeDataString(errMsg);
} //if (!newUrl.StartsWith("http://" + rs.ResponseUri.Host))
//{
// newUrl = string.Format("http://{0}{1}", rs.ResponseUri.Host, newUrl);
//} newUrl = "http://newrms.cjol.com/searchengine"; //var jr = GetResponseContent(rs); BaseUtil.OpenAdminPage(cjolCookies, new Uri(newUrl));
return null;
} private HttpWebResponse HttpPost(string url, Dictionary<string, object> postData)
{
string desKey = "!@#$%26)(*&^cjol<16>:|}{=-/*-+.CJOL@*&^%*()*<299>";
string logindataVal = HttpLoginCjolHelper.JScriptRun("jsencrypt", desKey, JsonConvert.SerializeObject(postData)).ToString(); //DesEncrypt(desKey, JsonConvert.SerializeObject(postData));
string postDataContent = "logindata=" + logindataVal;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] data = Encoding.UTF8.GetBytes(postDataContent);
request.ContentLength = data.Length;
request.AllowAutoRedirect = false;
request.CookieContainer = cjolCookies;
//request.Accept = "application/json";
request.Referer = "http://www.cjol.com/hr/";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36";
Stream myRequestStream = request.GetRequestStream();
myRequestStream.Write(data, 0, data.Length);
myRequestStream.Close();
myRequestStream = null; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); return response;
} private string GetResponseContent(HttpWebResponse response)
{
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close(); return retString;
} private string GetValidateCode(out string codekey)
{
codekey = null;
string validateCode = null;
string validateCodeKey = null;
var codeForm = new FrmValidateCode();
codeForm.ChangeValidateCode += (btn, args) =>
{
validateCodeKey = Guid.NewGuid().ToString().ToLower();
string codeimgUrl = "http://newrms.cjol.com/Common/ValidateCodePicture?Key=1&guid=" + validateCodeKey;
args.Data[0] = codeimgUrl; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(codeimgUrl);
request.Method = "GET";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36";
request.CookieContainer = cjolCookies;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
var codePic = new Bitmap(myResponseStream);
(args.Data[1] as PictureBox).Image = codePic;
myResponseStream.Close();
myResponseStream = null; }; codeForm.EnterValidateCode += (btn, args) =>
{
validateCode = args.Data[0].ToString();
if (!CheckValidateCode(validateCode))
{
args.Data[1] = "验证码校验失败!";
}
}; if (codeForm.ShowDialog() == DialogResult.OK)
{
codekey = validateCodeKey;
return validateCode;
} return null;
} private bool CheckValidateCode(string code)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://newrms.cjol.com/Account/Verification?txtValidateCode=" + code + "&rid=" + Guid.NewGuid().ToString("N"));
request.Method = "GET";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36";
request.Host = "newrms.cjol.com";
request.Referer = "http://www.cjol.com/hr/";
request.CookieContainer = cjolCookies;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string rs = GetResponseContent(response);
if (rs.Contains("(true)"))
{
return true;
}
else
{
return false;
}
} public static object JScriptRun(string jsMethodName, params object[] jsParams)
{
//编译的参数
var compiler = _provider.CreateCompiler();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
CompilerResults results = compiler.CompileAssemblyFromSource(parameters, jScriptClass);
Assembly assembly = results.CompiledAssembly; //动态编译脚本中的内容
Type _evaluateType = assembly.GetType("kyecjol.jsClass"); //执行指定的方法并传参数
object retObj = _evaluateType.InvokeMember(jsMethodName, BindingFlags.InvokeMethod,
null, null, jsParams);
return retObj;
} private const string jScriptClass = @"
package kyecjol { public class jsClass { public static function jsencrypt(k,e) {
var t = e.replace(/\\0/g, '')
,n = stringToHex(des(k, t, 1, 0));
//,n=stringToHex(k+e);
return n;
} public static function des(e, t, n, r,i) {
var s = 'charCodeAt', o = 'fromCharCode', u = new Array(16843776,0,65536,16843780,16842756,66564,4,65536,1024,16843776,16843780,1024,16778244,16842756,16777216,4,1028,16778240,16778240,66560,66560,16842752,16842752,16778244,65540,16777220,16777220,65540,0,1028,66564,16777216,65536,16843780,4,16842752,16843776,16777216,16777216,1024,16842756,65536,66560,16777220,1024,4,16778244,66564,16843780,65540,16842752,16778244,16777220,1028,66564,16843776,1028,16778240,16778240,0,65540,66560,0,16842756), a = new Array(-2146402272,-2147450880,32768,1081376,1048576,32,-2146435040,-2147450848,-2147483616,-2146402272,-2146402304,-134217728,-2147450880,1048576,32,-2146435040,1081344,1048608,-2147450848,0,-134217728,32768,1081376,-2146435072,1048608,-2147483616,0,1081344,32800,-2146402304,-2146435072,32800,0,1081376,-2146435040,1048576,-2147450848,-2146435072,-2146402304,32768,-2146435072,-2147450880,32,-2146402272,1081376,32,32768,-134217728,32800,-2146402304,1048576,-2147483616,1048608,-2147450848,-2147483616,1048608,1081344,0,-2147450880,32800,-134217728,-2146435040,-2146402272,1081344), f = new Array(520,134349312,0,134348808,134218240,0,131592,134218240,131080,134217736,134217736,131072,134349320,131080,134348800,520,134217728,8,134349312,512,131584,134348800,134348808,131592,134218248,131584,131072,134218248,8,134349320,512,134217728,134349312,134217728,131080,520,131072,134349312,134218240,0,512,131080,134349320,134218240,134217736,512,0,134348808,134218248,131072,134217728,134349320,8,131592,131584,134217736,134348800,134218248,520,134348800,131592,8,134348808,131584), l = new Array(8396801,8321,8321,128,8396928,8388737,8388609,8193,0,8396800,8396800,8396929,129,0,8388736,8388609,1,8192,8388608,8396801,128,8388608,8193,8320,8388737,1,8320,8388736,8192,8396928,8396929,129,8388736,8388609,8396800,8396929,129,0,0,8396800,8320,8388736,8388737,1,8396801,8321,8321,128,8396929,129,1,8192,8388609,8193,8396928,8388737,8193,8320,8388608,8396801,128,8388608,8192,8396928), c = new Array(256,34078976,34078720,1107296512,524288,256,1073741824,34078720,1074266368,524288,33554688,1074266368,1107296512,1107820544,524544,1073741824,33554432,1074266112,1074266112,0,1073742080,1107820800,1107820800,33554688,1107820544,1073742080,0,1107296256,34078976,33554432,1107296256,524544,524288,1107296512,256,33554432,1073741824,34078720,1107296512,1074266368,33554688,1073741824,1107820544,34078976,1074266368,256,33554432,1107820544,1107820800,524544,1107296256,1107820800,34078720,0,1074266112,1107296256,524544,33554688,1073742080,524288,0,1074266112,34078976,1073742080), h = new Array(536870928,541065216,16384,541081616,541065216,16,541081616,4194304,536887296,4210704,4194304,536870928,4194320,536887296,536870912,16400,0,4194320,536887312,16384,4210688,536887312,16,541065232,541065232,0,4210704,541081600,16400,4210688,541081600,536870912,536887296,16,541065232,4210688,541081616,4194304,16400,536870928,4194304,536887296,536870912,16400,536870928,541081616,4210688,541065216,4210704,541081600,0,541065232,16,16384,541065216,4210704,16384,4194320,536887312,0,541081600,536870912,4194320,536887312), p = new Array(2097152,69206018,67110914,0,2048,67110914,2099202,69208064,69208066,2097152,0,67108866,2,67108864,69206018,2050,67110912,2099202,2097154,67110912,67108866,69206016,69208064,2097154,69206016,2048,2050,69208066,2099200,2,67108864,2099200,67108864,2099200,2097152,67110914,67110914,69206018,69206018,2,2097154,67108864,67110912,2097152,69208064,2050,2099202,69208064,2050,67108866,69208066,69206016,2099200,0,2,69208066,0,2099202,69206016,2048,67108866,67110912,2048,2097154), d = new Array(268439616,4096,262144,268701760,268435456,268439616,64,268435456,262208,268697600,268701760,266240,268701696,266304,4096,64,268697600,268435520,268439552,4160,266240,262208,268697664,268701696,4160,0,0,268697664,268435520,268439552,266304,262144,266304,262144,268701696,4096,64,268697664,4096,266304,268439552,64,268435520,268697600,268697664,268435456,262144,268439616,0,268701760,262208,268435520,268697600,268439552,268439616,0,268701760,266240,266240,4160,4160,262208,268435456,268701696), v = des_createKeys(e), m = 0, g, y, b, w, E, S, x, T, N, C, k, L, A, O, M, _ = t.length, D = 0, P = v.length == 32 ? 3 : 9;
P == 3 ? N = n ? new Array(0,32,2) : new Array(30,-2,-2) : N = n ? new Array(0,32,2,62,30,-2,64,96,2) : new Array(94,62,-2,32,64,2,30,-2,-2),
t += '\0\0\0\0\0\0\0\0';
var result = '',
tempresult = ''; r == 1 && (C = i[s](m++) << 24 | i[s](m++) << 16 | i[s](m++) << 8 | i[s](m++),
L = i[s](m++) << 24 | i[s](m++) << 16 | i[s](m++) << 8 | i[s](m++),
m = 0); while (m < _) {
n ? (x = t[s](m++) << 16 | t[s](m++),
T = t[s](m++) << 16 | t[s](m++)) : (x = t[s](m++) << 24 | t[s](m++) << 16 | t[s](m++) << 8 | t[s](m++),
T = t[s](m++) << 24 | t[s](m++) << 16 | t[s](m++) << 8 | t[s](m++)),
r == 1 && (n ? (x ^= C,
T ^= L) : (k = C,
A = L,
C = x,
L = T)),
b = (x >>> 4 ^ T) & 252645135,
T ^= b,
x ^= b << 4,
b = (x >>> 16 ^ T) & 65535,
T ^= b,
x ^= b << 16,
b = (T >>> 2 ^ x) & 858993459,
x ^= b,
T ^= b << 2,
b = (T >>> 8 ^ x) & 16711935,
x ^= b,
T ^= b << 8,
b = (x >>> 1 ^ T) & 1431655765,
T ^= b,
x ^= b << 1,
x = x << 1 | x >>> 31,
T = T << 1 | T >>> 31;
for (var y = 0; y < P; y += 3) {
O = N[y + 1],
M = N[y + 2];
for (g = N[y]; g != O; g += M)
E = T ^ v[g],
S = (T >>> 4 | T << 28) ^ v[g + 1],
b = x,
x = T,
T = b ^ (a[E >>> 24 & 63] | l[E >>> 16 & 63] | h[E >>> 8 & 63] | d[E & 63] | u[S >>> 24 & 63] | f[S >>> 16 & 63] | c[S >>> 8 & 63] | p[S & 63]);
b = x,
x = T,
T = b;
}
x = x >>> 1 | x << 31,
T = T >>> 1 | T << 31,
b = (x >>> 1 ^ T) & 1431655765,
T ^= b,
x ^= b << 1,
b = (T >>> 8 ^ x) & 16711935,
x ^= b,
T ^= b << 8,
b = (T >>> 2 ^ x) & 858993459,
x ^= b,
T ^= b << 2,
b = (x >>> 16 ^ T) & 65535,
T ^= b,
x ^= b << 16,
b = (x >>> 4 ^ T) & 252645135,
T ^= b,
x ^= b << 4,
r == 1 && (n ? (C = x,
L = T) : (x ^= k,
T ^= A)),
n ? tempresult += String[o](x >>> 24, x >>> 16 & 255, x >>> 8 & 255, x & 255, T >>> 24, T >>> 16 & 255, T >>> 8 & 255, T & 255) : tempresult += String[o](x >>> 16 & 65535, x & 65535, T >>> 16 & 65535, T & 65535),
n ? D += 16 : D += 8,
D == 512 && (result += tempresult,
tempresult = '',
D = 0);
}
return result + tempresult;
} public static function des_createKeys(e) {
var t = 'charCodeAt',
pc2bytes0 = new Array(0,4,536870912,536870916,65536,65540,536936448,536936452,512,516,536871424,536871428,66048,66052,536936960,536936964),
pc2bytes1 = new Array(0,1,1048576,1048577,67108864,67108865,68157440,68157441,256,257,1048832,1048833,67109120,67109121,68157696,68157697),
pc2bytes2 = new Array(0,8,2048,2056,16777216,16777224,16779264,16779272,0,8,2048,2056,16777216,16777224,16779264,16779272),
pc2bytes3 = new Array(0,2097152,134217728,136314880,8192,2105344,134225920,136323072,131072,2228224,134348800,136445952,139264,2236416,134356992,136454144),
pc2bytes4 = new Array(0,262144,16,262160,0,262144,16,262160,4096,266240,4112,266256,4096,266240,4112,266256),
pc2bytes5 = new Array(0,1024,32,1056,0,1024,32,1056,33554432,33555456,33554464,33555488,33554432,33555456,33554464,33555488),
pc2bytes6 = new Array(0,268435456,524288,268959744,2,268435458,524290,268959746,0,268435456,524288,268959744,2,268435458,524290,268959746),
pc2bytes7 = new Array(0,65536,2048,67584,536870912,536936448,536872960,536938496,131072,196608,133120,198656,537001984,537067520,537004032,537069568),
pc2bytes8 = new Array(0,262144,0,262144,2,262146,2,262146,33554432,33816576,33554432,33816576,33554434,33816578,33554434,33816578),
pc2bytes9 = new Array(0,268435456,8,268435464,0,268435456,8,268435464,1024,268436480,1032,268436488,1024,268436480,1032,268436488),
pc2bytes10 = new Array(0,32,0,32,1048576,1048608,1048576,1048608,8192,8224,8192,8224,1056768,1056800,1056768,1056800),
pc2bytes11 = new Array(0,16777216,512,16777728,2097152,18874368,2097664,18874880,67108864,83886080,67109376,83886592,69206016,85983232,69206528,85983744),
pc2bytes12 = new Array(0,4096,134217728,134221824,524288,528384,134742016,134746112,16,4112,134217744,134221840,524304,528400,134742032,134746128),
pc2bytes13 = new Array(0,4,256,260,0,4,256,260,1,5,257,261,1,5,257,261);
var n = e.length >= 24 ? 3 : 1, r = new Array(32 * n), s = new Array(0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0), o, u, a = 0, f = 0, l,left,right; for (var c = 0; c < n; c++) {
left = e[t](a++) << 24 | e[t](a++) << 16 | e[t](a++) << 8 | e[t](a++),
right = e[t](a++) << 24 | e[t](a++) << 16 | e[t](a++) << 8 | e[t](a++),
l = (left >>> 4 ^ right) & 252645135,
right ^= l,
left ^= l << 4,
l = (right >>> -16 ^ left) & 65535,
left ^= l,
right ^= l << -16,
l = (left >>> 2 ^ right) & 858993459,
right ^= l,
left ^= l << 2,
l = (right >>> -16 ^ left) & 65535,
left ^= l,
right ^= l << -16,
l = (left >>> 1 ^ right) & 1431655765,
right ^= l,
left ^= l << 1,
l = (right >>> 8 ^ left) & 16711935,
left ^= l,
right ^= l << 8,
l = (left >>> 1 ^ right) & 1431655765,
right ^= l,
left ^= l << 1,
l = left << 8 | right >>> 20 & 240,
left = right << 24 | right << 8 & 16711680 | right >>> 8 & 65280 | right >>> 24 & 240,
right = l; for (var i = 0; i < s.length; i++)
s[i] ? (left = left << 2 | left >>> 26,
right = right << 2 | right >>> 26) : (left = left << 1 | left >>> 27,
right = right << 1 | right >>> 27),
left &= -15,
right &= -15,
o = pc2bytes0[left >>> 28] | pc2bytes1[left >>> 24 & 15] | pc2bytes2[left >>> 20 & 15] | pc2bytes3[left >>> 16 & 15] | pc2bytes4[left >>> 12 & 15] | pc2bytes5[left >>> 8 & 15] | pc2bytes6[left >>> 4 & 15],
u = pc2bytes7[right >>> 28] | pc2bytes8[right >>> 24 & 15] | pc2bytes9[right >>> 20 & 15] | pc2bytes10[right >>> 16 & 15] | pc2bytes11[right >>> 12 & 15] | pc2bytes12[right >>> 8 & 15] | pc2bytes13[right >>> 4 & 15],
l = (u >>> 16 ^ o) & 65535,
r[f++] = o ^ l,
r[f++] = u ^ l << 16;
}
return r; } public static function stringToHex(e) {
var t = 'charCodeAt'
, n = ''
, r = new Array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
for (var i = 0; i < e.length; i++)
n += r[e[t](i) >> 4] + r[e[t](i) & 15];
return n
} } }"; }
代码逻辑简要说明:
1.GetValidateCode方法:通过GET请求获取登录验证码,并显示在winform的界面上,以便用户可以参照输入,当然如果能够使用第三方打码工具自动识别验证码那就更简单了。
2.HttpPost方法:构建POST请求登录,并返回HttpWebResponse对象
3.从头部取得跳转的位置string newUrl = rs.Headers["Location"].ToString();如果是Default页面且包含message,则通过正则取到message信息并在winform界面上显示报错。
4.jScriptClass这个是从中国人才热线网站COPY的加密算法逻辑,JScriptRun就是执行JS加密算法并得到结果。
5.BaseUtil.OpenAdminPage方法:设置IE COOKIE,并用IE打开管理后台网址;
登录人才热线网站的关键点在于:JS加密及验证码
用法如下:
var httpLoginCjol = new HttpLoginCjolHelper();
string result = httpLoginCjol.Login(txtCjolUid.Text.Trim(), txtCjolPwd.Text);
if (!string.IsNullOrEmpty(result))
{
MessageBox.Show("登录中国人才热线网站失败,原因:" + result, "登录失败提示");
}
登录前程无忧:HttpLogin51JobHelper
public class HttpLogin51JobHelper
{
private CookieContainer web51jobCookies = new CookieContainer(); public string Login(string uName, string uId, string pwd)
{
string langType, accessKey, fksc, hidEhireGuid, hidRetUrl;
ReadHomePageData(out langType, out accessKey, out fksc, out hidEhireGuid, out hidRetUrl);
string loginUrl = "https://ehirelogin.51job.com/Member/UserLogin.aspx";
var rs = HttpPost(loginUrl, new Dictionary<string, string>
{
{"ctmName",uName},{"userName",uId},
{"password",pwd},{"checkCode",""},
{"oldAccessKey",accessKey},{"langtype",langType},
{"isRememberMe","false"},{"sc",fksc},
{"ec",hidEhireGuid},{"returl",hidRetUrl},
{"referrurl","http://ehire.51job.com/"}
}); string newUrl = rs.Headers["Location"].ToString(); // "http://ehire.51job.com/Candidate/SearchResumeIndexNew.aspx";
if (newUrl.IndexOf("errorId=", StringComparison.OrdinalIgnoreCase) > 0)
{
return GetLoginErrMsg(newUrl);
} //string rsString = GetResponseContent(rs); BaseUtil.OpenAdminPage(web51jobCookies, new Uri(newUrl)); return null;
} private void ReadHomePageData(out string langType, out string accessKey, out string fksc, out string hidEhireGuid, out string hidRetUrl)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://ehire.51job.com/");
request.Method = "GET";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36";
request.CookieContainer = web51jobCookies;
request.Host = "ehire.51job.com";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string rspContent = GetResponseContent(response); langType = "Lang=&Flag=1"; //getHiddenValueByElementId("hidLangType", rspContent);
accessKey = getHiddenValueByElementId("hidAccessKey", rspContent);
fksc = getHiddenValueByElementId("fksc", rspContent);
hidEhireGuid = getHiddenValueByElementId("hidEhireGuid", rspContent);
hidRetUrl = getHiddenValueByElementId("hidRetUrl", rspContent);
} private string GetLoginErrMsg(string errUrl)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(errUrl);
request.Method = "GET";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36";
request.CookieContainer = web51jobCookies;
request.Host = "ehire.51job.com";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string rspContent = GetResponseContent(response); string pattren = @"<div\s+id='errOther'.+<a>(?<msg>.+)</a></div>";
var regx = new System.Text.RegularExpressions.Regex(pattren, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string errMsg = regx.Match(rspContent).Groups["msg"].Value;
return errMsg;
} private HttpWebResponse HttpPost(string url, Dictionary<string, string> postData)
{ string postDataContent = null;
foreach (var kv in postData)
{
postDataContent += string.Format("&{0}={1}", kv.Key, kv.Value);
}
postDataContent = postDataContent.Substring(1); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] data = Encoding.UTF8.GetBytes(postDataContent);
request.ContentLength = data.Length;
request.AllowAutoRedirect = false;
request.CookieContainer = web51jobCookies;
request.Host = "ehire.51job.com";
request.Headers.Set("Origin", "http://ehire.51job.com");
request.Referer = "http://ehire.51job.com";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36";
Stream myRequestStream = request.GetRequestStream();
myRequestStream.Write(data, 0, data.Length);
myRequestStream.Close();
myRequestStream = null; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); return response;
} private string getHiddenValueByElementId(string eId, string body)
{
string pattren = "id=\"" + eId + "\"\\s+value=\"(?<evalue>\\w+)\"";
var regx = new System.Text.RegularExpressions.Regex(pattren, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if (regx.IsMatch(body))
{
return regx.Match(body).Groups["evalue"].Value;
} return string.Empty;
} private string GetResponseContent(HttpWebResponse response)
{
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close(); return retString;
}
}
代码逻辑简要说明:
1.ReadHomePageData方法:请求登录页面,获取隐藏域的字段信息
2.HttpPost方法:构建POST请求登录,并返回HttpWebResponse对象
3.从头部取得跳转的位置string newUrl = rs.Headers["Location"].ToString();如果包含errorId,则通过请求该error页面获取错误信息GetLoginErrMsg并在winform界面上显示报错。
4.BaseUtil.OpenAdminPage方法:设置IE COOKIE,并用IE打开管理后台网址;
登录前程无忧网站的关键点在于:取得登录页面的隐藏域的字段信息
用法如下:
var httpLogin51Job = new HttpLogin51JobHelper();
string result = httpLogin51Job.Login(txt51JobUName.Text.Trim(), txt51JobUid.Text.Trim(), txt51JobPwd.Text);
if (!string.IsNullOrEmpty(result))
{
MessageBox.Show("登录前程无忧网站失败,原因:" + result, "登录失败提示");
}
以下是如何设置IE COOKIE及通过IE打开指定网站的方法:
public static void OpenAdminPage(CookieContainer cookies, Uri rsUri)
{ foreach (Cookie cookie in cookies.GetCookies(rsUri)) //将cookie设置为浏览的cookie
{
InternetSetCookie(
"http://" + cookie.Domain.ToString(),
cookie.Name.ToString(),
cookie.Value.ToString() + ";expires=" + DateTime.UtcNow.AddDays(1).ToString("R"));
} System.Diagnostics.Process.Start("iexplore.exe", rsUri.AbsoluteUri); //打开浏览器 }
关于C#执行Javascript方法有如下几种:(我这里仅做一个汇总,可能不止这么多)
第一种:引用:Microsoft.JScript.DLL,然后使用:JScriptCodeProvider对象来动态编译jscript脚本(CompileAssemblyFromSource)生成程序集,最后通过这个JS程序集反射执行JS方法;参考说明:http://www.cnblogs.com/xdpxyxy/archive/2013/06/12/3132868.html
第二种:引用:Interop.MSScriptControl.dll,然后使用Eval执行脚本内容,参考说明:http://www.cnblogs.com/preacher/p/6347251.html 、 http://www.cnblogs.com/feiyuhuo/p/5474790.html
第三种:引用:开源项目 Javascript .NET(Noesis.Javascript.dll、msvcp100.dll、msvcr100.dll复制到bin目录下),然后使用JavascriptContext的Run方法执行,参考说明:http://www.cnblogs.com/yjmyzz/p/3359204.html
第四种:引用:Microsoft.JScript.Vsa(Microsoft.JScript、Microsoft.Vsa),然后使用Microsoft.JScript.Eval.JScriptEvaluate方法执行,参考说明:http://www.cnblogs.com/yjmyzz/p/3359204.html
第五种:在winform窗体中加入 WebBrowser 控件,然后将JS通过Url或直接设置DocumentText,最后利用webBrowser1.Document.InvokeScript方法执行WebBrowser 控件包含的JS方法,参考说明:http://blog.csdn.net/luxiaoyu_sdc/article/details/6896451 、 http://www.cnblogs.com/feiyuhuo/p/5474790.html (仅winform项目适用)
通过winform+模拟登录实现快速一键登录到人才招聘网站的更多相关文章
- Winform模拟post请求和get请求登录网站
引言 最近有朋友问如何用winform模拟post请求,然后登录网站,稍微想了一下,大致就是对http报文的相关信息的封装,然后请求网站登录地址的样子.发现自己的博客中对这部分只是也没总结,就借着这股 ...
- 一键登录怎么在iOS端实现?这篇文章教会你!
在一键登录出现之前,市场上最常见的APP 注册登录方式主要有账号密码.短信验证及第三方登录.这几种方式看似常见且便捷,实则存在许多安全隐患,用户体验也相对较差.首先,短信验证码到达率低.用户操作繁琐且 ...
- 使用uView UI+UniApp开发微信小程序--微信授权绑定和一键登录系统
在前面随笔<使用uView UI+UniApp开发微信小程序>和<使用uView UI+UniApp开发微信小程序--判断用户是否登录并跳转>介绍了微信小程序的常规登录处理和验 ...
- 速成KeePass全局自动填表登录QQ与迅雷(包括中文输入法状态时用中文用户名一键登录)
原文:http://bbs.kafan.cn/thread-1637531-1-1.html 使用目的:1 网页和本地客户端登录一站式解决2 通过KeePss修改密码和登录更方便,可以复制粘贴,省了输 ...
- Python+Selenium+PIL+Tesseract真正自动识别验证码进行一键登录
Python 2.7 IDE Pycharm 5.0.3 Selenium:Selenium的介绍及使用,强烈推荐@ Eastmount的博客 PIL : Pillow-3.3.0-cp27-cp27 ...
- php单点登录之模拟淘宝天猫同步登录
说到单点登录大家都很了解,一个站点登录其他域会自动登录. 单点登录SSO(Single Sign On)的方法有很多,比如:p3p.共享session.共享cookice.第三方OAuth认证. 这里 ...
- QQ一键登录功能的实现过程
QQ登录的思路: 当qq登陆成功后,QQ会给我们返回一个唯一的用户标识:openId,当用户授权QQ时,判断 if(已经有openId){ 跳转到登陆后的页面. }else if(没有openId){ ...
- 项目开发-->一键登录功能汇总
开发网站经常会提供一些一键登录功能,如:QQ.新浪微博.淘宝账号.开心网账号.人人网账号等进行快捷登录,下面记录几个常用的开放平台地址,方便以后开发需要. 1.QQ互联 2.新浪微博 网站接入QQ互联 ...
- Destoon QQ互联一键登录审核不通过的解决方案
在QQ互联上申请帐号之后提交了审核, 后台填写APPID和KEY之后自己申请的QQ号可以正常登录,但QQ互联审核的时候一直审核不通过说是“您的网站审核未通过,原因是“点击QQ登录按钮提示登录失败或出现 ...
随机推荐
- Unity 3D Framework Designing(4)——设计可复用的SubView和SubViewModel(Part 2)
在我们设计和开发应用程序时,经常要用到控件.比如开发一个客户端WinForm应用程序时,微软就为我们提供了若干控件,这些控件为我们提供了可被定制的属性和事件.属性可以更改它的外观,比如背景色,标题等, ...
- 《连载 | 物联网框架ServerSuperIO教程》- 17.支持实时数据库,高并发保存测点数据。附:3.4 发布与版本更新说明。
1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(一)System.out.print与Log
日常啰嗦 距离上一次更新博客有一段时间了,主要是因为最近有开发任务,另外,这段时间也在学习docker的相关知识,所以博客就没有继续写了,推荐一本书<Docker技术入门与实战>(第二版) ...
- python 调取 shell 命令的几种方法
os.system()无法获得到输出和返回值 os.popen()output = os.popen('cat /proc/cpuinfo')print output.read()返回的是 file ...
- 关于js参数传递矛盾新理解
之前看了很多人的解释,说js中,函数的参数传递都是值传递中不理解. 他们无非举了两个例子 在这两个例子中,第二个例子可以看出参数是由值传递的.因为函数内对象的变化没有影响到函数外对象的变化.但是在第一 ...
- 数据库DDL操作
DDL1. 数据库* 查看所有数据库:SHOW DATABASES* 切换(选择要操作的)数据库:USE 数据库名* 创建数据库:CREATE DATABASE [IF NOT EXISTS] myd ...
- Knockout Mvc Compoment FrameSet
Knockout Mvc Compoment FrameSet 框架文件结构 01- 网站(表现层),mvc主要作用视图展示. 02- 模型(Model),主要作用承载视图数据结构,网站前后台 ...
- Linux文件管理下
文件操作 对于文件,我们可以读取(read),写入(write)和运行(execute).读取是从已经存在的文件中获得数据.写入是向新的文件或者旧的文件写入数据.如果文件储存的是可执行的二进制码,那么 ...
- C语言求最小公倍数和最大公约数三种算法(经典)
把以前写的一些经验总结汇个总,方便给未来的学弟学妹们做个参考! --------------------------永远爱你们的:Sakura 最小公倍数:数论中的一种概念,两个整数公有的倍数成为他们 ...
- AndroidStudio升级后出现Refresh gradle project和connection timed out的原因和解决方法
笔者发现现在升级AndroidStudio不需要FQ了,于是在看到了升级提醒后手贱点击了升级.可悲剧的一幕发生了, 正在写的一个项目从上到下密密麻麻的错误,看了一下提示要求升级Gradle 那就升级吧 ...