'Passing Credentials To WebProxy?

I have a custom HTTP class for the service I am using. Eventually it will contain service specific requests in the form of methods. What I need to do is set the credentials of the proxy provided by the user, for example if the user has a proxy list.

Below is my code. I've commented the part I need to set credentials. I've looked at the iCredentials class on MSDN but I can't see how to set them from a string.

class RequestClass
{
    private CookieContainer cookieJar;
    private WebProxy proxy = null;

    public RequestClass()
    {
        this.cookieJar = new CookieContainer();
    }

    public RequestClass(String proxyURL, int port)
    {
        this.proxy = new WebProxy(proxyURL, port);
    }

    public RequestClass(String proxyURL, int port, String username, String password)
    {
        this.proxy = new WebProxy(proxyURL, port);
        // Need to set them here
    }

    // HTTP Get Request
    public HttpWebResponse getRequest(String url, NameValueCollection headers)
    {
        HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(url);
        getRequest.Method = "GET";
        getRequest.CookieContainer = cookieJar;

        foreach (String key in headers.Keys)
        {
            getRequest.Headers.Add(key, headers[key]);
        }

        return (HttpWebResponse)getRequest.GetResponse();
    }

    // HTTP Post Request
    public HttpWebResponse postRequest(String url, String postData, NameValueCollection headers)
    {
        byte[] postBytes = Encoding.ASCII.GetBytes(postData);

        HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(url);
        postRequest.Method = "POST";
        postRequest.CookieContainer = cookieJar;
        postRequest.ContentLength = postBytes.Length;
        postRequest.ProtocolVersion = HttpVersion.Version10;

        foreach(String key in headers.Keys)
        {
            postRequest.Headers.Add(key, headers[key]);
        }

        Stream postRequestStream = postRequest.GetRequestStream();

        postRequestStream.Write(postBytes, 0, postBytes.Length);
        postRequestStream.Close();

        return (HttpWebResponse)postRequest.GetResponse();
    }
}

}



Solution 1:[1]

I think this should work:

public RequestClass(String proxyURL, int port, String username, String password)
{
    //Validate proxy address
    var proxyURI = new Uri(string.Format("{0}:{1}", proxyURL, port));

    //Set credentials
    ICredentials credentials = new NetworkCredential(username, password);

    //Set proxy
    this.proxy =  = new WebProxy(proxyURI, true, null, credentials );
}

Solution 2:[2]

I made a little modification

public System.Net.IWebProxy RequestClass(String proxyURL, int port, String username, String password)
    {
        //Validate proxy address
        var proxyURI = new Uri(string.Format("{0}:{1}", proxyURL, port));

        //Set credentials
        ICredentials credentials = new NetworkCredential(username, password);

        //Set proxy
       return new WebProxy(proxyURI, true, null, credentials);
    }

Solution 3:[3]

Sklivvz's answer is correct. However, I disapprove of the string.Format usage in Uri

using System;
using System.Net;

public class RequestClass
{
    private WebProxy proxy = null;

    public RequestClass(string url, int port, string username, string password)
    {
        // Prepare URL
        var urlBuilder = new UriBuilder(url);
        urlBuilder.Port = port;

        // Setup credentials
        var credentials = new NetworkCredential(username, password);

        // Setup proxy
        this.proxy = new WebProxy(urlBuilder.Uri, true, null, credentials);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mironline
Solution 2 Syed Asad Ali Zaidi
Solution 3 DonSleza4e