1. using
2. using
3. using
4. using
5. using
6. using
7. using
8. using
9.
10. using
11. using
12. using
13. using
14.
15. namespace
16. {
17. public partial class
18. {
19. new
20. string
21.
22. public
23. {
24. InitializeComponent();
25. }
26.
27. private void Form1_Load( object
28. {
29. // 讀取登入頁面,擷取 VIEWSTATE 及 Cookies
30. byte [] bs = getBytesstring
31. "id=/"__VIEWSTATE/" value=/"" ; // viewState 的開始字元串
32. int
33. int end = html.IndexOf( "/""
34. // 擷取 viewState
35.
36. // 讀取圖檔校驗碼并顯示圖檔
37. , cc, null
38. new
39. new
40. // 顯示圖檔驗證碼
41. ms.Close();
42. }
43.
44. // 更新圖檔驗證碼
45. private void pictureBox1_DoubleClick( object
46. {
47. byte [] bs = getBytes , cc, null
48. new
49. new
50. // 更新圖檔驗證碼(可能看不清楚)
51. ms.Close();
52. }
53.
54. // 顯示 Cookies 資訊
55. private void button3_Click( object
56. {
57. new
58. new Uri(foreach (Cookie c in
59. {
60. " --> "
61. }
62. richTextBox1.Text = sb.ToString();
63. " Bytes"
64. }
65.
66. // 使用者登入
67. private void btnLogin_Click( object
68. {
69. try
70. {
71. string userName = txtUser.Text; // 擷取使用者名
72. string userPwd = txtPassword.Text; // 擷取密碼
73. string Verify = txtVerify.Text; // 擷取驗證碼
74. string postData = "__VIEWSTATE=" + HttpUtility.UrlEncode(viewState) + "&ctl00$CPH_Content$tb_LoginNameOrLoginEmail=" + userName + "&ctl00$CPH_Content$tb_Password=" + userPwd + "&ctl00$CPH_Content$tb_ExPwd=" + Verify + "&ctl00$CPH_Content$Image_Login.x=16&ctl00$CPH_Content$Image_Login.y=17" ; //&from=";
75. byte [] buffer = Encoding.Default.GetBytes(postData); // 要發送的資料
76. string html = Encoding.UTF8.GetString(buffer); // 擷取傳回的頁面内容
77.
78. if (html.IndexOf( "您好,您已經成功登入。"
79. {
80. "你已成功登入 CSDN"
81. }
82. else
83. {
84. "登入 CSDN 失敗!"
85. }
86. }
87. catch
88. {
89. MessageBox.Show(Err.Message);
90. }
91. }
92.
93. // 讀取網絡資源,傳回位元組數組
94. private static byte [] getBytes( string url, CookieContainer cookie, byte
95. {
96. int c = url.IndexOf( "/"
97. byte [] data = null
98. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
99. request.CookieContainer = cookie;
100. request.Referer = (c > 0 ? url.Substring(0, c) : url);
101. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
102. "gzip, deflate"
103.
104. if (postData != null ) // 需要 Post 資料
105. {
106. "POST"
107. "application/x-www-form-urlencoded"
108. request.ContentLength = postData.Length;
109. Stream requestStream = request.GetRequestStream();
110. requestStream.Write(postData, 0, postData.Length);
111. requestStream.Close();
112. }
113.
114. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
115. string
116. int ContentLength = ( int
117. Stream s = response.GetResponseStream();
118. c = 1024 * 10;
119. if (ContentLength < 0) // 不能擷取資料的長度
120. {
121. new byte
122. new
123. int
124. while
125. {
126. ms.Write(data, 0, l);
127. l = s.Read(data, 0, c);
128. }
129. data = ms.ToArray();
130. ms.Close();
131. }
132. else // 資料長度已知
133. {
134. new byte
135. int
136. while
137. {
138. int
139. pos += l;
140. ContentLength -= l;
141. }
142. }
143. s.Close();
144. response.Close();
145.
146. if (ce == "gzip" ) // 若資料是壓縮格式,則要進行解壓
147. {
148. new MemoryStream(); // 解壓後的流
149. new MemoryStream(data); // 用于解壓的流
150. new
151. byte [] buffer = new byte [c]; // 讀資料緩沖區
152. int l = g.Read(buffer, 0, c); // 一次讀 10K
153. while
154. {
155. js.Write(buffer, 0, l);
156. l = g.Read(buffer, 0, c);
157. }
158. g.Close();
159. ms.Close();
160. data = js.ToArray();
161. js.Close();
162. }
163. return data; // 傳回位元組數組
164. }
165. }
166. }