日期:2014-05-18 浏览次数:20901 次
public class HttpHelper { public static CookieContainer Cookies = new CookieContainer(); public static string GetHttpResponse(string url, string postdata) { try { HttpWebRequest MyRequest = (HttpWebRequest)WebRequest.Create(url); MyRequest.Method = "POST"; MyRequest.ContentLength = postdata.Length; MyRequest.CookieContainer = Cookies; MyRequest.KeepAlive = true; MyRequest.AllowAutoRedirect = true; MyRequest.ContentType = "application/x-www-form-urlencoded"; MyRequest.UserAgent = " Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1"; MyRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; if (postdata != null) { ASCIIEncoding MyEncoding = new ASCIIEncoding(); byte[] MyByte = MyEncoding.GetBytes(postdata); Stream MyStream = MyRequest.GetRequestStream(); MyStream.Write(MyByte, 0, postdata.Length); MyStream.Close(); } string getContent = null; Encoding myEncoding = Encoding.GetEncoding("UTF-8"); HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse(); if (MyResponse.StatusCode == HttpStatusCode.OK) { /*获取响应页面cookie*/ // MyResponse.Cookies = MyRequest.CookieContainer.GetCookies(MyRequest.RequestUri); MessageBox.Show("adfdfasdf"); Stream MyNewStream = MyResponse.GetResponseStream(); StreamReader MyStreamReader = new StreamReader(MyNewStream, myEncoding); getContent = MyStreamReader.ReadToEnd(); MyStreamReader.Close(); } MyResponse.Close(); return getContent; } catch (Exception) { return string.Empty; } } }