Friday, August 29, 2014

Revisiting REST Proxy

In June I published a blog post about creating a class for easily consuming data from RESTful web services (http://geek-goddess-bonnie.blogspot.com/2014/06/proxy-class-for-consuming-restful.html). Since then, I've had the need to use credentials to access a REST service, so my class has had to change a bit. Not much, but I thought it was enough to warrant a new blog post, so here it is. Basically all I did was add UserName and Password properties, plus another constructor like this:

public ProxyBaseREST(string baseAddress, string userName, string password) : this(baseAddress)
{
    this.UserName = userName;
    this.Password = password;
}

And the GetJSON() method was modified slightly to add the credentials to the WebClient:

WebClient WC = new WebClient();
if (!string.IsNullOrEmpty(this.UserName) && !string.IsNullOrEmpty(this.Password))
    WC.Credentials = new NetworkCredential(this.UserName, this.Password);

And the rest is the same as in my earlier post. Pretty easy, eh?

No comments:

Post a Comment