I have a json URl like “https://landfill.bugzilla.org/bugzilla-tip/jsonrpc.cgi?method=Product.get¶ms=%5B{“ids”:”4″}]”
I want to pass this as URL in c# program. below is the code snippet. How can i pass the arguments like ids as above?
try
{
string url="https://landfill.bugzilla.org/bugzilla-tip/jsonrpc.cgi?method=Product.get";
string ret = string.Empty;
StreamWriter requestWriter;
var webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
if (webRequest != null)
{
webRequest.Method = "GET";
webRequest.ServicePoint.Expect100Continue = false;
webRequest.Timeout = 20000;
webRequest.ContentType = "application/json";
}
HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
Stream resStream = resp.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
ret = reader.ReadToEnd();
return ret;
}
catch (WebException exception)
{
string responseText;
using (var reader = new StreamReader(exception.Response.GetResponseStream()))
{
responseText = reader.ReadToEnd();
}
return responseText;
}
}
Need to pass “ids” as argument, please help.
If you know how Url looks like and sure that argument are Url-safe (like
int) you can simply use String.Format to construct it:Note that this is not good way of contructing Url – it only would be ok for one-time-use code and when you know that inserted parameters are Url-safe. Proper approach would be to use Uri class or approaches from How to build a query string for a URL in C#?
If you need ot construct more complex parameter (like array of IDs) – use jbehren suggestion for JSON serializtion.