开发者社区> 技术小牛人> 正文

通过winform+模拟登录实现快速一键登录到人才招聘网站

简介:
+关注继续查看

之前为了便于人事部门招聘登录网站更简洁高效,免去每天频繁输网址、用户名、密码等相关登录信息,特基于winform+HttpWebRequest实现模拟请求登录,最终达到一键登录到招聘网站后台的效果。

要实现一键登录到各大人才招聘网站就必需先了解网站的登录步骤即原理,然后通过代码一步步模拟实现即可。

通过我对前程无忧、中国人才热线、中华英才网 三个网站的登录研究,找出基本相同的以下几个步聚:

1.请求登录主页面,然后获取隐藏域的字段信息;

2.构建登录请求数据,有的网站是form表单请求,有的网站是:json请求

3.请求后获取到登录的Cookie,然后通过设置IE的Cookie,最后打开IE浏览器及指定的管理后台地址即可。

除了上述三大步骤以外,其实还涉及一个重要的知识点,那就是:C#调用javascript脚本并获得执行的结果,因为各网站中有用到js生成的加密数据,而这些js加密方法我们无法直接联想到对应的c#方法,故必需采用js方法获取加密数据,这个在本文后面我会讲解几种方法。

上面已经分析了一键登录到人才招聘网站的原理,下面就分别贴出:前程无忧、中国人才热线、 这二个网站的登录实现代码,并作简要说明,以便大家学习与参考:

(PS:注意实现一键登录只是为了便于人事提高网站的使用效率,请不要利用我的代码作非法的事情,否则后果自负,同时也不排除后续这些网站会改变登录方式,那么这些登录就都会失效的)

登录中国人才热线:HttpLoginCjolHelper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
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<stringobject> {
        "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<stringobject> 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,
                    nullnull, 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加密及验证码

用法如下:

1
2
3
4
5
6
var httpLoginCjol = new HttpLoginCjolHelper();
string result = httpLoginCjol.Login(txtCjolUid.Text.Trim(), txtCjolPwd.Text);
if (!string.IsNullOrEmpty(result))
{
    MessageBox.Show("登录中国人才热线网站失败,原因:" + result, "登录失败提示");
}

  

登录前程无忧:HttpLogin51JobHelper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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<stringstring>
        {
            {"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<stringstring> 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打开管理后台网址;

登录前程无忧网站的关键点在于:取得登录页面的隐藏域的字段信息

用法如下:

1
2
3
4
5
6
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打开指定网站的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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项目适用)

本文转自 梦在旅途 博客园博客,原文链接:http://www.cnblogs.com/zuowj/p/6855610.html  ,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
阿里云服务器ECS登录用户名是什么?系统不同默认账号也不同
阿里云服务器Windows系统默认用户名administrator,Linux镜像服务器用户名root
12624 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,云吞铺子总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系统盘、创建快照、配置安全组等操作如何登录ECS云服务器控制台? 1、先登录到阿里云ECS服务器控制台 2、点击顶部的“控制台” 3、通过左侧栏,切换到“云服务器ECS”即可,如下图所示 通过ECS控制台的远程连接来登录到云服务器 阿里云ECS云服务器自带远程连接功能,使用该功能可以登录到云服务器,简单且方便,如下图:点击“远程连接”,第一次连接会自动生成6位数字密码,输入密码即可登录到云服务器上。
32345 0
阿里云ECS云服务器初始化设置教程方法
阿里云ECS云服务器初始化是指将云服务器系统恢复到最初状态的过程,阿里云的服务器初始化是通过更换系统盘来实现的,是免费的,阿里云百科网分享服务器初始化教程: 服务器初始化教程方法 本文的服务器初始化是指将ECS云服务器系统恢复到最初状态,服务器中的数据也会被清空,所以初始化之前一定要先备份好。
13996 0
阿里云服务器安全组设置内网互通的方法
虽然0.0.0.0/0使用非常方便,但是发现很多同学使用它来做内网互通,这是有安全风险的,实例有可能会在经典网络被内网IP访问到。下面介绍一下四种安全的内网互联设置方法。 购买前请先:领取阿里云幸运券,有很多优惠,可到下文中领取。
18107 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
17756 0
5723
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
OceanBase 入门到实战教程
立即下载
阿里云图数据库GDB,加速开启“图智”未来.ppt
立即下载
实时数仓Hologres技术实战一本通2.0版(下)
立即下载