http://www.oschina.net/code/snippet_111708_16530
C#自动登录开源中国
[代码] [C#]代码
001 | using System; |
002 | using System.Collections.Generic; |
003 | using System.IO; |
004 | using System.Net; |
005 | using System.Text; |
006 | using System.Web.Security; |
007 |
008 | namespace HttpOnlyCookieReader |
009 | { |
010 | public class OschinaLogin |
011 | { |
012 | #region Request相关 |
013 |
014 | // 创建Http请求 |
015 | public static HttpWebRequest CreateRequest( string url) |
016 | { |
017 | if (url == null ) |
018 | return null ; |
019 |
020 | HttpWebRequest request = null ; |
021 | try |
022 | { |
023 | request = (HttpWebRequest)WebRequest.Create(url); |
024 | } |
025 | catch (Exception e) |
026 | { |
027 | } |
028 |
029 | return request; |
030 | } |
031 |
032 | // 伪装为一般浏览器 |
033 | public static void DisguiseBrower(HttpWebRequest request) |
034 | { |
035 | request.KeepAlive = true ; |
036 | request.UserAgent = |
037 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11" ; |
038 | } |
039 |
040 | // 设置请求的Host属性 |
041 | public static void SetHost(HttpWebRequest request, string host) |
042 | { |
043 | request.Host = host; |
044 | } |
045 |
046 | // 设置Cookies |
047 | public static void SetCookies(HttpWebRequest request, Dictionary< string , string > cookieNameValueDict) |
048 | { |
049 | CookieContainer cookieContainer = new CookieContainer(); |
050 | if (cookieNameValueDict != null && cookieNameValueDict.Count > 0) |
051 | { |
052 | foreach (var key in cookieNameValueDict.Keys) |
053 | { |
054 | cookieContainer.Add( new Cookie(key, cookieNameValueDict[key])); |
055 | } |
056 | } |
057 | request.CookieContainer = cookieContainer; |
058 | } |
059 |
060 | // 写入Post参数 |
061 | public static void WritePostParams(HttpWebRequest request, string postData) |
062 | { |
063 | if ( string .IsNullOrEmpty(postData)) |
064 | return ; |
065 |
066 | request.Method = "Post" ; |
067 | request.ContentType = "application/x-www-form-urlencoded" ; |
068 |
069 | byte [] b = Encoding.Default.GetBytes(postData); |
070 | request.ContentLength = b.Length; |
071 | request.AutomaticDecompression = DecompressionMethods.GZip; |
072 |
073 | System.IO.Stream sw = null ; |
074 | try |
075 | { |
076 | sw = request.GetRequestStream(); |
077 | sw.Write(b, 0, b.Length); |
078 | } |
079 | catch (System.Exception ex) |
080 | { |
081 | } |
082 | finally |
083 | { |
084 | if (sw != null ) |
085 | { |
086 | sw.Close(); |
087 | } |
088 | } |
089 | } |
090 |
091 | #endregion |
092 |
093 |
094 | #region Response相关 |
095 |
096 | // 获取响应 |
097 | public static HttpWebResponse GetResponse(HttpWebRequest request) |
098 | { |
099 | HttpWebResponse response = null ; |
100 | try |
101 | { |
102 | response = (HttpWebResponse)request.GetResponse(); |
103 | } |
104 | catch (Exception e) |
105 | { |
106 | } |
107 |
108 | return response; |
109 | } |
110 |
111 | public static void PrintCookies(HttpWebResponse response) |
112 | { |
113 | if (response == null ) |
114 | return ; |
115 |
116 | Console.WriteLine( string .Format( "\n响应网页 {0} 的Cookie如下:" , response.ResponseUri.AbsoluteUri)); |
117 | foreach (Cookie cookie in response.Cookies) |
118 | { |
119 | Console.WriteLine( "Cookie name: " + cookie.Name); |
120 | Console.WriteLine( "Cookie value: " + cookie.Value); |
121 | Console.WriteLine( "Cookie path: " + cookie.Path); |
122 | Console.WriteLine( "Cookie secure: " + cookie.Secure); |
123 | Console.WriteLine( "Cookie httponly: " + cookie.HttpOnly); |
124 | Console.WriteLine( "Cookie timestamp: " + cookie.TimeStamp); |
125 | } |
126 | } |
127 |
128 | public static void PrintResponseContent(HttpWebResponse response) |
129 | { |
130 | if (response == null ) |
131 | return ; |
132 |
133 | Console.WriteLine( string .Format( "\n响应网页 {0} 内容如下:" , response.ResponseUri.AbsoluteUri)); |
134 | string retContent = "" ; |
135 | StreamReader streamReader = null ; |
136 | try |
137 | { |
138 | streamReader = new StreamReader(response.GetResponseStream(), Encoding.Default); |
139 | retContent = streamReader.ReadToEnd(); |
140 | Console.WriteLine(retContent); |
141 | } |
142 | catch (IOException e) |
143 | { |
144 | } |
145 | finally |
146 | { |
147 | if (streamReader != null ) |
148 | { |
149 | try |
150 | { |
151 | streamReader.Close(); |
152 | } |
153 | catch (IOException e) |
154 | { |
155 | } |
156 | } |
157 | } |
158 | } |
159 |
160 | // 打印响应状态信息 |
161 | public static void PrintResponseStatusInfo(HttpWebResponse response) |
162 | { |
163 | if (response == null ) |
164 | return ; |
165 |
166 | Console.WriteLine( string .Format( "\n响应网页 {0} 的状态信息如下:" , response.ResponseUri.AbsoluteUri)); |
167 | Console.WriteLine( string .Format( "Status code: {0}" , response.StatusCode)); |
168 | Console.WriteLine( string .Format( "Status description: {0}" , response.StatusDescription)); |
169 | } |
170 |
171 | public static void PrintResponseHeaders(HttpWebResponse response) |
172 | { |
173 | if (response == null ) |
174 | return ; |
175 |
176 | Console.WriteLine( string .Format( "\n响应网页 {0} 的头信息如下:" , response.ResponseUri.AbsoluteUri)); |
177 | WebHeaderCollection headers = response.Headers; |
178 | foreach (var key in headers.AllKeys) |
179 | { |
180 | Console.WriteLine( string .Format( "{0}={1}" , key, headers[key])); |
181 | } |
182 | } |
183 |
184 | #endregion |
185 | |
186 |
187 | #region 加密函数 |
188 |
189 | // MD5或SHA1加密 |
190 | public static string EncryptPassword( string pwdStr, string pwdFormat) |
191 | { |
192 | string EncryptPassword = null ; |
193 | if ( "SHA1" .Equals(pwdFormat.ToUpper())) |
194 | { |
195 | EncryptPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(pwdStr, "SHA1" ); |
196 | } |
197 | else if ( "MD5" .Equals(pwdFormat.ToUpper())) |
198 | { |
199 | EncryptPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(pwdStr, "MD5" ); |
200 | } |
201 | else |
202 | { |
203 | EncryptPassword = pwdStr; |
204 | } |
205 | return EncryptPassword; |
206 | } |
207 |
208 | #endregion |
209 |
210 |
211 | #region OSChina登录函数 |
212 |
213 | public static void LoginOsChina( string email, string pwd) |
214 | { |
215 | // Request部分 |
216 | HttpWebRequest request = CreateRequest( "http://www.oschina.net/action/user/hash_login" ); |
217 | SetHost(request, "www.oschina.net" ); // 设置请求Host属性 |
218 | DisguiseBrower(request); // 伪装为正常浏览器 |
219 | SetCookies(request, null ); // 登录OSChina不需要Cookie |
220 | WritePostParams(request, string .Format( "email={0}&pwd={1}" , email, EncryptPassword(pwd, "SHA1" ))); // OSChina密码为40位SHA1加密结果 |
221 |
222 | // Response部分 |
223 | HttpWebResponse response = GetResponse(request); |
224 | PrintCookies(response); // 打印Cookie信息 |
225 | PrintResponseContent(response); // 打印响应网页内容 |
226 | PrintResponseStatusInfo(response); // 打印响应元信息 |
227 | PrintResponseHeaders(response); // 打印响应头信息 |
228 | } |
229 |
230 | #endregion |
231 |
232 | public static void Main( string [] args) |
233 | { |
234 | LoginOsChina( "你的注册邮箱" , "你的登录密码" ); |
235 | |
236 | Console.ReadLine(); |
237 | } |
238 | } |
239 | } |