public string HttpGetRequest(string url, bool followRedirect = true) { Is403Error = false; RequestStatusCode = 0; CurrentRetryCount = 0; WebClientRequest = (HttpWebRequest)WebRequest.Create(url); WebClientRequest.CookieContainer = _cJar; WebClientRequest.UserAgent = WebUserAgentString; WebClientRequest.Accept = "*/*"; WebClientRequest.KeepAlive = false; WebClientRequest.Method = "GET"; if (followRedirect) { WebClientRequest.AllowAutoRedirect = false; } while (!CheckWebResponse()) { Thread.Sleep(5000); CurrentRetryCount++; Log.Info($"HTTP Get Retry: {CurrentRetryCount} of {MaxWebRetries}"); if (CurrentRetryCount >= MaxWebRetries) { return(string.Empty); } } if (followRedirect && (RequestStatusCode == (int)HttpStatusCode.Moved || RequestStatusCode == (int)HttpStatusCode.Found)) { while (WebClientResponse.StatusCode == HttpStatusCode.Found || WebClientResponse.StatusCode == HttpStatusCode.Moved) { WebClientResponse.Close(); WebClientRequest = (HttpWebRequest)WebRequest.Create(WebClientResponse.Headers["Location"]); WebClientRequest.AllowAutoRedirect = false; WebClientRequest.CookieContainer = _cJar; WebClientResponse = (HttpWebResponse)WebClientRequest.GetResponse(); } } StreamReader = new StreamReader(WebClientResponse.GetResponseStream() ?? throw new InvalidOperationException()); RequestStatusCode = (int)WebClientResponse.StatusCode; var responseData = StreamReader.ReadToEnd(); return(responseData); }
public string HttpPostRequest(string url, string post, bool followRedirect = true, string refer = "") { WebClientRequest = (HttpWebRequest)WebRequest.Create(url); WebClientRequest.CookieContainer = _cJar; WebClientRequest.UserAgent = WebUserAgentString; WebClientRequest.KeepAlive = false; WebClientRequest.Method = "POST"; WebClientRequest.Referer = refer; if (followRedirect) { WebClientRequest.AllowAutoRedirect = false; } var postBytes = Encoding.ASCII.GetBytes(post); WebClientRequest.ContentType = "text/x-www-form-urlencoded"; WebClientRequest.ContentLength = postBytes.Length; var requestStream = WebClientRequest.GetRequestStream(); requestStream.Write(postBytes, 0, postBytes.Length); requestStream.Close(); WebClientResponse = (HttpWebResponse)WebClientRequest.GetResponse(); if (followRedirect && (WebClientResponse.StatusCode == HttpStatusCode.Moved || WebClientResponse.StatusCode == HttpStatusCode.Found)) { while (WebClientResponse.StatusCode == HttpStatusCode.Found || WebClientResponse.StatusCode == HttpStatusCode.Moved) { WebClientResponse.Close(); WebClientRequest = (HttpWebRequest)WebRequest.Create(WebClientResponse.Headers["Location"]); WebClientRequest.AllowAutoRedirect = false; WebClientRequest.CookieContainer = _cJar; WebClientResponse = (HttpWebResponse)WebClientRequest.GetResponse(); } } StreamReader = new StreamReader(WebClientResponse.GetResponseStream() ?? throw new InvalidOperationException()); return(StreamReader.ReadToEnd()); }