Wednesday, 31 August 2016

C#: Pass multiple values to a REST endpoint without having multiple parameters

Instead of passing the parameters to /REST.svc?param1=a&param2=b

var items = new NameValueCollection();
items.Add("param1", "a");
items.Add("param2", "b");

var content = HttpPost("http://MyRestEndpoint", items);

public static string HttpPost(string uri, NameValueCollection pairs)
            byte[] response = null;
            using (WebClient client = new WebClient())
                response = client.UploadValues(uri, pairs);
            return System.Text.Encoding.UTF8.GetString(response);

NOTE: You can quite easily inject a proxy using the following code:
client.Proxy = new System.Net.WebProxy("myProxy", 1001);

No comments:

Post a comment