日期:2014-05-18 浏览次数:20781 次
HttpWebRequest httpWebRequest = null; HttpWebResponse httpWebResponse = null; var boundary = "---------------" + DateTime.Now.Ticks.ToString("x"); var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n"); var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n"); try { byte[] byteRequest = Encoding.Default.GetBytes(postData); httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.binzhou.cc/ershou_Rental.asp?act=save"); httpWebRequest.CookieContainer = cookieContainer; httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary; httpWebRequest.ServicePoint.ConnectionLimit = maxTry; httpWebRequest.Referer = "http://www.binzhou.cc/ershou_Rental.asp"; httpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */* " httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"; httpWebRequest.Method = "POST"; httpWebRequest.ContentLength = byteRequest.Length; httpWebRequest.AllowAutoRedirect = true; Stream stream = httpWebRequest.GetRequestStream(); stream.Write(byteRequest, 0, byteRequest.Length); stream.Close(); try { httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); } catch (WebException ex) { httpWebResponse = (HttpWebResponse)ex.Response; } //httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); httpWebResponse.Cookies = cookieContainer.GetCookies(httpWebRequest.RequestUri); Stream responseStream = httpWebResponse.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream, encoding); //cc.SetCookies(httpWebRequest.RequestUri); cc = cookieContainer; if (!(PublishVoid.htLogin.Contains(url))) PublishVoid.htLogin.Add(url, cc); string html = streamReader.ReadToEnd(); streamReader.Close(); responseStream.Close(); currentTry = 0; httpWebRequest.Abort(); httpWebResponse.Close(); return html; }