C Sharp .NET Configuration for the Proxy

This article describes configuration of the Proxy Host for the C# .NET HTTP client. C# works well with dynamic websites.

ProxyMesh supports configuration of the Proxy Host with Basic Authentication as well as IP address authentication. With the latter, you don't need to use a username and password.

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 a code sample for making HTTP requests through a proxy using C#. However, of the two Proxy authentication methods -- Basic and IP address -- we strongly recommend the use of IP authentication for HTTPS requests.

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);
}

Web Browser Proxy Settings

For web browsers, you must use IP address authentication, then configure your network proxy settings.

Web browsers provide their own methods to configure the proxy settings. Please see our article How to Change Web Browser Proxy Settings for links to video tutorials and help articles for changing the proxy settings in major web browsers. If you use Firefox or Chrome, the FoxyProxy plugin makes it very easy to configure your proxy settings.

Here is an additional link to a tutorial on changing the proxy server in C#.

cURL in C# .NET

To use libcurl functions in C# .NET, you can access them through CurlSharp, a .NET binding and object-oriented wrapper for libcurl.

The libcurl web-client library provides cross-platform .NET applications for easy implementation.

Follow this link for details.

Selenium and C#

For automated testing, Visual Studio contains WebDriver's C# bindings. Many users select NUnit as their testing framework. You'll also need the ChromeDriver executable.

These components and their interactions are discussed in a how-to article, Getting Started with WebDriver in C# Using Visual Studio.

Still need help? Contact Us Contact Us