|
- internal class CHIUser
- {
- private string Username,Password;
- private HttpWebRequest req;
- private HttpWebResponse res;
- private CookieContainer cookies;
- internal void SetDefaultRequest(HttpWebRequest req,string Method="POST")
- {
- req.Accept = "*/*";
- req.Headers.Set(HttpRequestHeader.AcceptLanguage, "zh-cn");
- req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
- req.ContentType = "application/x-www-form-urlencoded";
- req.Headers.Set("Cache-Control", "no-cache");
- req.Method = Method;
- req.Timeout = 8000;
- req.KeepAlive = true;
- }
- internal CHIUser(string UN, string PW,out bool suc)
- {
- Username = UN;
- Password = PW;
- cookies = new CookieContainer();
- try
- {
- req = (HttpWebRequest)WebRequest.Create("http://www.258ch.com/");
- SetDefaultRequest(req,"GET");
- req.CookieContainer = cookies;
- req.GetResponse();
- req = (HttpWebRequest)
- WebRequest.Create("http://www.258ch.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1");
- SetDefaultRequest(req);
- req.CookieContainer = cookies;
- string poststr=
- "username=" +
- GBKEncoding(UN) + "&password=" + PW +
- "&quickforward=yes&handlekey=ls";
- byte[] postdata = Encoding.Unicode.GetBytes(poststr);
- Stream st = req.GetRequestStream();
- st.Write(postdata, 0, postdata.Length);
- st.Close();
- res = (HttpWebResponse)req.GetResponse();
- StreamReader sr = new StreamReader(res.GetResponseStream());
- MessageBox.Show(sr.ReadToEnd());
- sr.Close();
- foreach (Cookie c in cookies.GetCookies(new Uri("http://www.258ch.com/")))
- {
- MessageBox.Show(c.Name +"="+ c.Value);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- suc = false;
- return;
- }
- suc = true;
- }
- internal string GBKEncoding(string text)
- {
- byte[] strByte = Encoding.Default.GetBytes(text);
- StringBuilder sb = new StringBuilder();
- foreach (var by in strByte)
- {
- if ((by >= 48 && by <= 57) || (by >= 65 && by <= 90) || (by >= 97 && by <= 122))
- { sb.Append((char)by);}
- else{sb.Append('%'+by.ToString("X")); }
- }
- return sb.ToString();
- }
- }
- }
复制代码 |
|