这篇文章主要为大家详细介绍了C#使用HttpWebRequest与HttpWebResponse模拟用户登录,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要为大家详细介绍了C#使用HttpWebRequest与HttpWebResponse模拟用户登录,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要为大家详细介绍了C#使用HttpWebRequest与HttpWebResponse模拟用户登录,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
模拟艺龙旅游网登录,供大家参考,具体内容如下
想模拟登录,首先整理一下流程
1.通过360浏览器(ie,火狐等等)f12开发人员工具抓到相关数据
2.获取验证码(拿到cookie),登录时也需要使用
3.登录
f12调出开发人员工具,输入用户名,密码登录,看我们抓到了什么信息。

request url:这个就是登录请求的url
https://secure.elong.com/passport/ajax/elonglogin
方式post
form data:这个是我们要post传输的数据:
username=xzdylyh&passwd=12313&validatecode=验证码&rememberme=false
其它一些重要信息在request headers中
*****************************************************************
我使用c# 设计的winform界面

复制代码
|
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
|
using system;using system.collections.generic;using system.linq;using system.text;using system.threading.tasks;using system.web;using system.net;using system.io;using system.data;namespace httphelper{ public class elogn_login { public static cookiecontainer container = null; //存储验证码cookie #region 登录 public string requestm(string uname,string passwd,string vaildate) { httpwebrequest request = null; httpwebresponse response = null; try { request = (httpwebrequest)httpwebrequest.create("https://secure.elong.com/passport/ajax/elonglogin"); request.method = "post"; request.contenttype = "application/x-www-form-urlencoded; charset=utf-8"; request.useragent = "mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/45.0.2454.101 safari/537.36"; request.cookiecontainer = container;//获取验证码时候获取到的cookie会附加在这个容器里面 request.keepalive = true;//建立持久性连接 //整数据 string postdata = string.format("username={0}&passwd={1}&validatecode={2}&rememberme=true", uname, passwd, vaildate); asciiencoding encoding = new asciiencoding(); byte[] bytepostdata = encoding.getbytes(postdata); request.contentlength = bytepostdata.length; //发送数据 using结束代码段释放 using (stream requeststm = request.getrequeststream()) { requeststm.write(bytepostdata, 0, bytepostdata.length); } //响应 response = (httpwebresponse)request.getresponse(); string text = string.empty; using (stream responsestm = response.getresponsestream()) { streamreader redstm = new streamreader(responsestm, encoding.utf8); text = redstm.readtoend(); } return text; } catch (exception ex) { var msg = ex.message; return msg; } } #endregion #region 获取验证码 public stream getcodestream(string codeurl) { //验证码请求 httpwebrequest request = (httpwebrequest)webrequest.create(codeurl); request.method = "get"; request.contenttype = "application/x-www-form-urlencoded"; request.useragent = "mozilla/5.0 (windows nt 6.1; wow64; rv:5.0.1) gecko/20100101 firefox/5.0.1"; request.accept = "image/webp,*/*;q=0.8"; request.cookiecontainer = new cookiecontainer();//!very important.!!! container = request.cookiecontainer; var c = request.cookiecontainer.getcookies(request.requesturi); httpwebresponse response = (httpwebresponse)request.getresponse(); response.cookies = container.getcookies(request.requesturi); stream stream = response.getresponsestream(); return stream; } } #endregion} |
|
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
|
using system;using system.collections.generic;using system.componentmodel;using system.data;using system.drawing;using system.linq;using system.text;using system.threading.tasks;using system.windows.forms;using system.io;using httphelper;namespace windowsformsapplication8{ public partial class elong_login_form : form { public elong_login_form() { initializecomponent(); } private void button1_click(object sender, eventargs e) { elogn_login elonglogin = new elogn_login(); var rmsg = elonglogin.requestm(txtusername.text,txtpassword.text,txtvaildata.text); messagebox.show(rmsg); } private void elong_login_form_load(object sender, eventargs e) { reflshpicimage();//更新验证码 } //更新验证码 public void reflshpicimage() { string codeurl = "https://secure.elong.com/passport/getvalidatecode"; elogn_login agent = new elogn_login(); stream stmimage = agent.getcodestream(codeurl); picvalidate.image = image.fromstream(stmimage); } private void btnrevalidate_click(object sender, eventargs e) { reflshpicimage();//更新验证码 } private void picvalidate_click(object sender, eventargs e) { reflshpicimage();//更新验证码 } }} |
最后执行效果,登录的session已经成功返回。

发表评论