文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>c# winform实现网页上用户自动登陆,模拟网站登录

c# winform实现网页上用户自动登陆,模拟网站登录

时间:2010-08-19  来源:lcqjiyi

using System; 
002 using System.Collections.Generic; 
003 using System.Text; 
004 using System.Net; 
005 using System.IO;
006   
007 namespace Czt.Web 
008
009     /// <summary> 
010     /// 实现网站登录类 
011     /// </summary> 
012     public class Post 
013     { 
014         /// <summary> 
015         /// 网站Cookies 
016         /// </summary> 
017         private string _cookieHeader = string.Empty; 
018         public string CookieHeader 
019         { 
020             get 
021             { 
022                 return _cookieHeader; 
023             } 
024             set 
025             { 
026                 _cookieHeader = value; 
027             } 
028         } 
029         /// <summary> 
030         /// 网站编码 
031         /// </summary> 
032         private string _code = string.Empty; 
033         public string Code 
034         { 
035             get { return _code; } 
036             set { _code = value; } 
037         }
038   
039    
040         private string _pageContent = string.Empty; 
041         public string PageContent 
042         { 
043             get { return _pageContent; } 
044             set { _pageContent = value; } 
045         }
046   
047         private Dictionary<string, string> _para = new Dictionary<string, string>(); 
048         public Dictionary<string, string> Para 
049         { 
050             get { return _para; } 
051             set { _para = value; } 
052         }
053   
054    
055         /**/ 
056         /// <summary> 
057         /// 功能描述:模拟登录页面,提交登录数据进行登录,并记录Header中的cookie 
058         /// </summary> 
059         /// <param name="strURL">登录数据提交的页面地址</param> 
060         /// <param name="strArgs">用户登录数据</param> 
061         /// <param name="strReferer">引用地址</param> 
062         /// <param name="code">网站编码</param> 
063         /// <returns>可以返回页面内容或不返回</returns> 
064         public string PostData(string strURL, string strArgs, string strReferer, string code, string method) 
065         { 
066             return  PostData(strURL,  strArgs,  strReferer,  code,  method, string.Empty); 
067         } 
068         public string PostData(string strURL, string strArgs, string strReferer, string code, string method, string contentType) 
069         { 
070             try 
071             { 
072                 string strResult = ""; 
073                 HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL); 
074                 myHttpWebRequest.AllowAutoRedirect = true; 
075                 myHttpWebRequest.KeepAlive = true; 
076                 myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*"; 
077                 myHttpWebRequest.Referer = strReferer;
078   
079    
080                 myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
081   
082                 if (string.IsNullOrEmpty(contentType)) 
083                 { 
084                     myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; 
085                 } 
086                 else 
087                 { 
088                     myHttpWebRequest.ContentType = "contentType"; 
089                 }
090   
091                 myHttpWebRequest.Method = method;
092   
093                 myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
094   
095                 if (myHttpWebRequest.CookieContainer == null) 
096                 { 
097                     myHttpWebRequest.CookieContainer = new CookieContainer(); 
098                 }
099   
100                 if (this.CookieHeader.Length > 0) 
101                 { 
102                     myHttpWebRequest.Headers.Add("cookie:" + this.CookieHeader); 
103                     myHttpWebRequest.CookieContainer.SetCookies(new Uri(strURL), this.CookieHeader); 
104                 }
105   
106    
107   
108                 byte[] postData = Encoding.GetEncoding(code).GetBytes(strArgs); 
109                 myHttpWebRequest.ContentLength = postData.Length;
110   
111                 System.IO.Stream PostStream = myHttpWebRequest.GetRequestStream(); 
112                 PostStream.Write(postData, 0, postData.Length); 
113                 PostStream.Close();
114   
115                 HttpWebResponse response = null; 
116                 System.IO.StreamReader sr = null; 
117                 response = (HttpWebResponse)myHttpWebRequest.GetResponse();
118   
119    
120   
121                 if (myHttpWebRequest.CookieContainer != null) 
122                 { 
123                     this.CookieHeader = myHttpWebRequest.CookieContainer.GetCookieHeader(new Uri(strURL)); 
124                 }
125   
126                 sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding(code));    //    //utf-8 
127                 strResult = sr.ReadToEnd(); 
128                 sr.Close(); 
129                 response.Close(); 
130                 return strResult; 
131             } 
132             catch (Exception ex) 
133             { 
134                 Utilities.Document.Create("C:\\error.log", strArgs, true, Encoding.UTF8); 
135             } 
136             return string.Empty; 
137         }
138   
139         /**/ 
140         /// <summary> 
141         /// 功能描述:在PostLogin成功登录后记录下Headers中的cookie,然后获取此网站上其他页面的内容 
142         /// </summary> 
143         /// <param name="strURL">获取网站的某页面的地址</param> 
144         /// <param name="strReferer">引用的地址</param> 
145         /// <returns>返回页面内容</returns> 
146         public string GetPage(string strURL, string strReferer, string code) 
147         { 
148             return GetPage(strURL, strReferer,code,string.Empty); 
149         } 
150         public string GetPage(string strURL, string strReferer,string code,string contentType) 
151         { 
152             string strResult = ""; 
153             HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL); 
154             myHttpWebRequest.AllowAutoRedirect = true; 
155             myHttpWebRequest.KeepAlive = false; 
156             myHttpWebRequest.Accept = "*/*"; 
157             myHttpWebRequest.Referer = strReferer; 
158             myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
159   
160             myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)"; 
161             if (string.IsNullOrEmpty(contentType)) 
162             { 
163                 myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; 
164             } 
165             else 
166             { 
167                 myHttpWebRequest.ContentType = contentType; 
168             } 
169             myHttpWebRequest.Method = "GET";
170   
171             if (myHttpWebRequest.CookieContainer == null) 
172             { 
173                 myHttpWebRequest.CookieContainer = new CookieContainer(); 
174             }
175   
176             if (this.CookieHeader.Length > 0) 
177             { 
178                 myHttpWebRequest.Headers.Add("cookie:" + this.CookieHeader); 
179                 myHttpWebRequest.CookieContainer.SetCookies(new Uri(strURL), this.CookieHeader); 
180             }
181   
182    
183             HttpWebResponse response = null; 
184             System.IO.StreamReader sr = null; 
185             response = (HttpWebResponse)myHttpWebRequest.GetResponse();
186   
187    
188             Stream streamReceive; 
189             string gzip = response.ContentEncoding;
190   
191             if (string.IsNullOrEmpty(gzip) || gzip.ToLower() != "gzip") 
192             { 
193                 streamReceive = response.GetResponseStream(); 
194             } 
195             else 
196             { 
197                 streamReceive = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress); 
198             }
199   
200             sr = new System.IO.StreamReader(streamReceive, Encoding.GetEncoding(code));
201   
202             if (response.ContentLength > 1) 
203             { 
204                 strResult = sr.ReadToEnd(); 
205             } 
206             else 
207             { 
208                 char[] buffer=new char[256]; 
209                 int count = 0; 
210                 StringBuilder sb = new StringBuilder(); 
211                 while ((count = sr.Read(buffer, 0, buffer.Length)) > 0) 
212                 { 
213                     sb.Append(new string(buffer)); 
214                 } 
215                 strResult = sb.ToString(); 
216             } 
217             sr.Close(); 
218             response.Close(); 
219             return strResult; 
220         }
221   
222     } 
223 }
相关阅读 更多 +
排行榜 更多 +
暗影鬼灭之刃

暗影鬼灭之刃

动作格斗 下载
龙族模拟器内置作弊菜单

龙族模拟器内置作弊菜单

模拟经营 下载
恋爱人生模拟器最新版2024下载安装

恋爱人生模拟器最新版2024下载安装

角色扮演 下载