public static string GetCookie(string requestUrlString, Encoding encoding, ref CookieContainer cookie) { //向服务端请求 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.CookieContainer = new CookieContainer(); //将请求的结果发送给客户端(界面、应用) HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); cookie.Add(myResponse.Cookies); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), encoding); return reader.ReadToEnd(); } public static string GetHtml(string requestUrlString, Encoding encoding, CookieContainer cookie) { string ua = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"; HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.UserAgent = ua; myRequest.CookieContainer = cookie; HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), encoding); return reader.ReadToEnd(); } public static string PostLogin(string postData, string requestUrlString, ref CookieContainer cookie) { ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(postData); //向服务端请求 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = data.Length; myRequest.CookieContainer = new CookieContainer(); Stream newStream = myRequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); //将请求的结果发送给客户端(界面、应用) HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); cookie.Add(myResponse.Cookies); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); return reader.ReadToEnd(); } public static string PostRequest(string postData, string requestUrlString, CookieContainer cookie) { ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(postData); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = data.Length; myRequest.CookieContainer = cookie; Stream newStream = myRequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); return reader.ReadToEnd(); }
作者:黄聪
出处:http://www.cnblogs.com/huangcong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/huangcong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。