-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReqUtil.cs
93 lines (85 loc) · 3.25 KB
/
ReqUtil.cs
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
using System.Net.Http;
namespace autoInternet
{
public class ReqUtil
{
private HttpClient client;
public ReqUtil()
{
this.client = new HttpClient();
this.client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
}
public string getLoginMsg()
{
string url = "http://192.168.2.135";
var response = this.client.GetAsync(url);
try
{
var text = response.Result.Content.ReadAsStringAsync();
var s = text.Result;
var m = System.Text.RegularExpressions.Regex.Match(s, @"href='(\S+)'");
return m.Groups[1].ToString().Split('?')[1];
}
catch (System.Exception)
{
return "你已在线";
throw;
}
}
public string login(string username, string password)
{
string p = this.getLoginMsg();
if (p == "你已在线")
{
return p;
}
string url = "http://192.168.2.135/eportal/InterFace.do?method=login";
System.Collections.Generic.Dictionary<string, string> data =
new System.Collections.Generic.Dictionary<string, string>();
data.Add("userId", username);
data.Add("password", password);
data.Add("service", "internet");
data.Add("queryString", p);
data.Add("operatorPwd", "");
data.Add("operatorUserId", "");
data.Add("validcode", "");
data.Add("passwordEncrypt", "false");
FormUrlEncodedContent formdata = new FormUrlEncodedContent(data);
var res = this.client.PostAsync(url, formdata);
var text = res.Result.Content.ReadAsStringAsync();
return text.Result;
}
public string getLogoutMsg()
{
string url = "http://192.168.2.135/eportal/InterFace.do?method=getOnlineUserInfo";
try
{
var res = this.client.GetAsync(url);
var text = res.Result.Content.ReadAsStringAsync();
var m = System.Text.RegularExpressions.Regex.Match(text.Result, "\"userIndex\":\"(\\w+)\"");
return m.Groups[1].ToString();
}
catch (System.Exception)
{
return "已处于下线";
throw;
}
}
public string logout()
{
string url = "http://192.168.2.135/eportal/InterFace.do?method=logout";
string userIndex = this.getLogoutMsg();
if (userIndex == "已处于下线")
{
return userIndex;
}
System.Collections.Generic.Dictionary<string, string> data =
new System.Collections.Generic.Dictionary<string, string>();
data.Add("userIndex", userIndex);
FormUrlEncodedContent formdata = new FormUrlEncodedContent(data);
var res = this.client.PostAsync(url, formdata);
var text = res.Result.Content.ReadAsStringAsync();
return text.Result;
}
}
}