This article describes configuration of the Proxy Host for the C# .NET HTTP client. C# works well with dynamic websites.
The following configuration example code for C# .NET was provided by Michael Eisenstein:
WebProxy ProxyString = new WebProxy("http://PROXYHOST:PORT", true); //set network credentials may be optional NetworkCredential proxyCredential = new NetworkCredential("USERNAME", "PASSWORD"); ProxyString.Credentials = proxyCredential; WebRequest.DefaultWebProxy = ProxyString; HttpWebRequest request = (HttpWebRequest); //manually set authorization header string authInfo = "USERNAME" + ":" + "PASSWORD"; authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); request.Headers["Proxy-Authorization"] = "Basic " + authInfo;
The following section of this article presents a more complete code example.
Making Requests in C#
Below is another code sample for making HTTP requests through a proxy using C#.
string proxyUri = string.Format("{0}:{1}", proxyAddress, proxyPort); NetworkCredential proxyCreds = new NetworkCredential( proxyUserName, proxyPassword ); WebProxy proxy = new WebProxy(proxyUri, true) { UseDefaultCredentials = false, Credentials = proxyCreds, }; HttpClient client = null; cookieContainer = new CookieContainer(); httpClientHandler = new HttpClientHandler() { Proxy = proxy, PreAuthenticate = true, UseDefaultCredentials = false, UseProxy = true, UseCookies = true, CookieContainer = cookieContainer }; client = new HttpClient(httpClientHandler); int _TimeoutSec = 90; client.Timeout = new TimeSpan(0, 0, _TimeoutSec); client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:55.0) Gecko/20100101 Firefox/55.0"); client.DefaultRequestHeaders.Add("Accept", "text/html, */*; q=0.01"); client.DefaultRequestHeaders.Add("Accept-Language", "gzip, deflate, br"); client.DefaultRequestHeaders.Accept. Add(new MediaTypeWithQualityHeaderValue ("application/x-www-form-urlencoded")); try { HttpResponseMessage response = await client.GetAsync(TARGETURL); HttpContent content = response.Content; // ... Check Status Code Console.WriteLine("Response StatusCode: " + (int)response.StatusCode); // ... Read the string. string result = await content.ReadAsStringAsync(); } catch (Exception ex) { Console.WriteLine(ex.Message); }
Related posts:
Proxy Configuration Examples – Java
You can use below example code to access to the service. import org.apache.http.HttpHost; import org.apache.http.client.fluent.*; public class Example { public static void main(String[] args) t...
You can use below example code to access to the service. import org.apache.http.HttpHost; import org.apache.http.client.fluent.*; public class Example { public static void main(String[] args) t...
Proxy Configuration Examples – wget
Wget Configuration for the Proxy This article describes the GNU Wget free software package for retrieving files from...
Wget Configuration for the Proxy This article describes the GNU Wget free software package for retrieving files from...
Proxy Configuration Examples – JavaScript
Request Library JavaScript has a Request library designed for simplicity and ease in making HTTP calls. It supports HTTPS and follows redirects by default. Here is the basic request s...
Request Library JavaScript has a Request library designed for simplicity and ease in making HTTP calls. It supports HTTPS and follows redirects by default. Here is the basic request s...
Proxy Configuration Examples – cURL
cURL Configuration for the Proxy This article addresses configuration of cURL and libcurl for the proxy. cU...
cURL Configuration for the Proxy This article addresses configuration of cURL and libcurl for the proxy. cU...
Proxy Configuration Examples - Python
Why you need ProxyEgg for your web scraping projects? If you are extracting data from the web at scale, you’ve probably already figured out the answer. IP banning. The website you are targeting might...
Why you need ProxyEgg for your web scraping projects? If you are extracting data from the web at scale, you’ve probably already figured out the answer. IP banning. The website you are targeting might...