http://rookiesite.com/node?destination=node
尝试了几个办法 都失败了 不知道错在哪里 求解
public CookieCollection cookie = new CookieCollection(); string post(string url, string postData) { ASCIIEncoding encoding = new ASCIIEncoding(); byte[] bytesToPost = encoding.GetBytes(postData); CookieContainer cookieCon = new CookieContainer(); if (cookie.Count > 0) { cookieCon.Add(new Uri(url), cookie); } HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url); httpRequest.CookieContainer = cookieCon; httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; httpRequest.ContentType = "application/x-www-form-urlencoded"; httpRequest.Method = "POST"; httpRequest.ContentLength = bytesToPost.Length; Stream requestStream = httpRequest.GetRequestStream(); requestStream.Write(bytesToPost, 0, bytesToPost.Length); requestStream.Close(); Stream responseStream = httpRequest.GetResponse().GetResponseStream(); string stringResponse = string.Empty; StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8); stringResponse = responseReader.ReadToEnd(); responseStream.Close(); cookie = httpRequest.CookieContainer.GetCookies(new Uri(url)); return stringResponse; }
public CookieCollection cookie = new CookieCollection(); string post(string url, string postData) { ASCIIEncoding encoding = new ASCIIEncoding(); byte[] bytesToPost = encoding.GetBytes(postData); CookieContainer cookieCon = new CookieContainer(); if (cookie.Count > 0) { cookieCon.Add(new Uri(url), cookie); } HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url); httpRequest.CookieContainer = cookieCon; httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; httpRequest.ContentType = "application/x-www-form-urlencoded"; httpRequest.Method = "POST"; httpRequest.ContentLength = bytesToPost.Length; Stream requestStream = httpRequest.GetRequestStream(); requestStream.Write(bytesToPost, 0, bytesToPost.Length); requestStream.Close(); Stream responseStream = httpRequest.GetResponse().GetResponseStream(); string stringResponse = string.Empty; StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8); stringResponse = responseReader.ReadToEnd(); responseStream.Close(); cookie = httpRequest.CookieContainer.GetCookies(new Uri(url)); return stringResponse; }
这两个办法POST后返回的还是需要登录 求解
post 方法没有问题,问题可能出在header上面,例如缺少referer,cookie等,建议查看下用浏览器登录时的post请求header
不行 还是失败了 这个之前考虑过的