forked from X-rus/xNet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.cs
146 lines (114 loc) · 3.91 KB
/
example.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
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
public class Captcha
{
public string Url { get; private set; }
public string Key { get; set; }
public Captcha(string url)
{
Url = url;
}
}
public enum AccountStatus
{
None,
Valid,
NotValid
}
public class Message
{
public string AuthorID { get; set; }
public string AuthorName { get; set; }
public string Value { get; set; }
}
public class Account : IHttpConnect
{
public HttpRequest Request { get; set; }
public string Login { get; private set; }
public string Password { get; private set; }
public CookieDictionary Cookies { get; private set; }
public Account(string login, string password)
{
Login = login;
Password = password;
}
public void InitRequest(HttpRequest request)
{
Request = request;
request.Cookies = Cookies; // На случай, если аккаунт уже авторизован.
request.AllowAutoRedirect = false;
request.Referer = "http://www.site.com";
request.BaseAddress = new Uri("http://www.site.com");
}
public AccountStatus Logining(ref Captcha captcha)
{
// Если это первое обращение, то устанавливаем новые куки.
if (captcha == null)
{
Cookies = new CookieDictionary();
Request.Cookies = Cookies;
}
Request.AddParam("login", Login).AddParam("password", Password);
// Если до этого требовался ввод капчи.
if (captcha != null)
{
Request.AddParam("c_key", captcha.Key);
}
string loginingResult = Request.Post("/logining.php").ToString();
// Если требуется ввод капчи.
if (loginingResult.Contains("need_captcha"))
{
string captchaUrl = loginingResult.Substring("captcha_url=", "&");
captcha = new Captcha(captchaUrl);
return AccountStatus.None;
}
if (loginingResult.Contains("ok"))
{
return AccountStatus.Valid;
}
return AccountStatus.NotValid;
}
public Message[] GetPrivateMessages()
{
string getPmResult = Request.Post("/get_pm.php").ToString();
var messages = new List<Message>();
// парсим список личных сообщений
return messages.ToArray();
}
public void ReplyToMessage(string userId, string message)
{
Request.AddParam("to_id", userId).AddParam("msg", message);
Request.Post("/send_pm.php").None();
}
}
static void Main()
{
using (var request = new HttpRequest())
{
request.UserAgent = HttpHelper.ChromeUserAgent();
request.Proxy = HttpProxyClient.Parse("127.0.0.18080");
var account = new Account("John", "Smith");
account.InitRequest(request);
Captcha captcha = null;
AccountStatus accountStatus = AccountStatus.None;
do
{
accountStatus = account.Logining(ref captcha);
// Нужно ввести капчу.
if (captcha != null)
{
request.Get(captcha.Url).ToFile("cap.jpg");
captcha.Key = Console.ReadLine();
}
} while (captcha != null);
if (accountStatus == AccountStatus.Valid)
{
var messages = account.GetPrivateMessages();
foreach (var message in messages)
{
string answer = string.Format(
"Привет, {0}! Мой хозяин сейчас занят и не может ответить тебе...",
message.AuthorName);
account.ReplyToMessage(message.AuthorID, answer);
}
}
}
}