-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUrl.cs
178 lines (174 loc) · 5.05 KB
/
Url.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
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Drawing;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
using System.Diagnostics;
namespace JiaowuHelper
{
class Url
{
private static string UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36";
public static Uri GetTrueUrl(string Url)
{
//设置网址时获取教务网重定向的页面,并且保存Cookie
HttpWebResponse ResponseObject = null;
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(Url);
HttpWReq.CookieContainer = new CookieContainer();
HttpWReq.UserAgent = UserAgent;
HttpWReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
HttpWReq.KeepAlive = true;
ResponseObject = (HttpWebResponse)HttpWReq.GetResponse();
if (ResponseObject == null)
{
return null;
}
string loginPgae = readHtml(ResponseObject.GetResponseStream());
Login.get().setloginPage(loginPgae);
writeFile(Login.get().loginPage, "E:\\login.html");
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
return null;
}
finally
{
if (ResponseObject != null)
ResponseObject.Close();
}
Login.get().cookie = ResponseObject.Cookies;
return ResponseObject.ResponseUri;
}
public static Image GetImageWithCookie(string url)
{
//获取一张图片
try
{
Image img = Image.FromStream(getPageStream(url));
return img;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
return null;
}
}
public static bool PostLogin(string form)
{
//进行登录验证
try
{
Stream stream = getPostStream(Login.get().loginActionPage, form);
if (stream == null) return false;
string result = readHtml(stream);
writeFile(result, @"E:\\post.html");
if (Login.get().PageInfoParse(result) == false)
{
//获取登录错误提示
Regex reg = new Regex("<script language='javascript' defer>alert\\('(.+?)'\\);document.getElementById");
Match match = reg.Match(result);
if (match.Value != "")
Login.get().LoginError = match.Value.Substring("<script language='javascript' defer>alert('".Length, match.Value.Length - "<script language='javascript' defer>alert('".Length - "');document.getElementById".Length);
return false;
}
return true;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
return false;
}
}
public static Stream getPostStream(string url, string postData)
{
//提交一个POST表单到指定页面
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(url);
HttpWReq.CookieContainer = new CookieContainer();
HttpWReq.CookieContainer.Add(Login.get().cookie);
HttpWReq.UserAgent = UserAgent;
HttpWReq.Method = "POST";
HttpWReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
HttpWReq.KeepAlive = true;
HttpWReq.ContentType = "application/x-www-form-urlencoded";
HttpWReq.Referer = url;
byte[] postBytes = Encoding.ASCII.GetBytes(postData);
HttpWReq.ContentLength = postBytes.Length;
using (Stream reqStream = HttpWReq.GetRequestStream())
{
//写入POST表单
reqStream.Write(postBytes, 0, postBytes.Length);
}
HttpWebResponse ResponseObject = (HttpWebResponse)HttpWReq.GetResponse();
return ResponseObject.GetResponseStream();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
return null;
}
}
public static string readHtml(Stream stream)
{
//读取HTML网页并关闭流
if (stream == null) return "";
MemoryStream destination = new MemoryStream();
StreamCopy(stream,destination);
stream.Close();
return Encoding.GetEncoding("gb2312").GetString(destination.ToArray());
}
private static void StreamCopy(Stream src, MemoryStream destination)
{
byte[] array = new byte[81920];
int count;
while ((count = src.Read(array, 0, array.Length)) != 0)
{
destination.Write(array, 0, count);
}
}
public static void writeFile(string str, string file)
{
#if DEBUG
try
{
StreamWriter sw = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.ReadWrite), Encoding.UTF8);
sw.Write(str);
sw.Close();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
#endif
}
public static Stream getPageStream(string url)
{
Login login = Login.get();
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(url);
HttpWReq.CookieContainer = new CookieContainer();
if (login.cookie != null)
HttpWReq.CookieContainer.Add(login.cookie);
HttpWReq.UserAgent = UserAgent;
HttpWReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
HttpWReq.KeepAlive = true;
HttpWReq.Referer = url;
HttpWebResponse ResponseObject = (HttpWebResponse)HttpWReq.GetResponse();
return ResponseObject.GetResponseStream();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
return null;
}
}
}
}