日期:2014-05-17 浏览次数:20952 次
public string GetHtml(string url, string refUrl, string postData, bool isPost, CookieContainer cookieContainer) { if (string.IsNullOrEmpty(postData)) { return GetHtml(url, cookieContainer); } byte[] byteRequest = Encoding.Default.GetBytes(postData); HttpWebRequest httpWebRequest; httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); httpWebRequest.CookieContainer = cookieContainer; httpWebRequest.ContentType = contentType; httpWebRequest.Referer = refUrl; httpWebRequest.Accept = accept; httpWebRequest.UserAgent = userAgent; httpWebRequest.Method = isPost ? "POST" : "GET"; httpWebRequest.ContentLength = byteRequest.Length; Stream stream = httpWebRequest.GetRequestStream(); stream.Write(byteRequest, 0, byteRequest.Length); stream.Close(); HttpWebResponse httpWebResponse; httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); Stream responseStream = httpWebResponse.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream, encoding); string html = streamReader.ReadToEnd(); streamReader.Close(); responseStream.Close(); return html; }
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();