日期:2014-05-18 浏览次数:21201 次
Test.HttpHeader header = new Test.HttpHeader(); header.accept = "itext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; header.contentType = "application/x-www-form-urlencoded"; header.method = "POST"; header.userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"; header.maxTry = 300; CookieCollection mycookie = Test.HTMLHelper.GetCookieCollection("http://www.renren.com/PLogin.do", // "mail=bgaidu%40163.com&password=123456789&origURL=https%3A%2F%2Fsafe.renren.com%2F&domain=renren.com&formName=&method=&isplogin=true&submit=%E7%99%BB%E5%BD%95", header);
HttpWebRequest request = null; HttpWebResponse response = null; try { CookieContainer cc = new CookieContainer(); request = (HttpWebRequest)WebRequest.Create(loginUrl); request.Method = header.method; request.ContentType = header.contentType; byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata); request.ContentLength = postdatabyte.Length; request.AllowAutoRedirect = false; request.CookieContainer = cc; request.KeepAlive = true; //提交请求 Stream stream; stream = request.GetRequestStream(); stream.Write(postdatabyte, 0, postdatabyte.Length); stream.Close(); //接收响应 response = (HttpWebResponse)request.GetResponse(); response.Cookies = request.CookieContainer.GetCookies(request.RequestUri); CookieCollection cook = response.Cookies; //Cookie字符串格式 string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri); return cook; } catch (Exception ex) { throw ex; }