public string BeginPostSend(string posturl, Encoding encoding, string UserAgent, string Accept, string AcceptLanguage, string proxyURL, string postData) { string contentResult = string.Empty; string err = string.Empty; int couterTry = 0; byte[] data = encoding.GetBytes(postData); char[] charArray = new char[2048]; do { // Set some request parameter // So as to readability, don't put below parameter set into a lonely function this.request = WebRequest.Create(posturl) as HttpWebRequest; this.request.CookieContainer = this.cookieContainer; this.request.AllowAutoRedirect = true; this.request.Method = "POST"; this.request.Accept = Accept; this.request.Headers.Add("Accept-Language", AcceptLanguage); this.request.Headers.Add("Accept-Encoding", "gzip, deflate"); this.request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore); this.request.KeepAlive = true; this.request.ContentLength = data.Length; this.request.ContentType = "application/x-www-form-urlencoded"; this.request.UserAgent = UserAgent; this.request.Timeout = 60000; //Define a Proxy Object WebProxy proxy = new WebProxy(); //Server address and port proxy.Address = new Uri(proxyURL); //User and Password //proxy.Credentials = new NetworkCredential("", ""); //Launch web Authentication this.request.UseDefaultCredentials = true; //Set proxy this.request.Proxy = proxy; try { couterTry++; if (couterTry > 1) { Thread.Sleep(6000); } // Put post data into stream this.outStream = request.GetRequestStream(); this.outStream.Write(data, 0, data.Length); // Send request and get relative response data using (this.response = request.GetResponse() as HttpWebResponse) using (this.inStream = this.response.GetResponseStream()) using (this.SR = new StreamReader(this.inStream, encoding)) // Not untill request.GetResponse(), program begin sending Post request to destination Web page { if (this.response.StatusCode == HttpStatusCode.OK) { streamDelegate SRDelegate = new streamDelegate(this.SR.ReadToEnd); try { IAsyncResult asyncR = SRDelegate.BeginInvoke(null, null); asyncR.AsyncWaitHandle.WaitOne(); contentResult = SRDelegate.EndInvoke(asyncR); } catch { this.DisposeStream(); continue; } //contentResult = tempResult.ToString(); this.DisposeStream(); break; } else { this.DisposeStream(); contentResult = string.Empty; if (couterTry >= 10) { break; } } } } catch (Exception ex) { this.DisposeStream(); contentResult = string.Empty; err = ex.Message; if (couterTry >= 10) { break; } } } while (true); this.DisposeStream(); return contentResult; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)