How to call REST API from a console application?
The response from my REST service will be XML format.
In web I am calling like this
string url = string.Format("{0}/name?PrimaryName={1}", ConfigurationManager.AppSettings["URLREST"], txtName.Text);
string details= CallRestMethod(url);
public string CallRestMethod(string url)
{
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.Method = "GET";
webrequest.ContentType = "application/x-www-form-urlencoded";
webrequest.Headers.Add("Username", "xyz");
webrequest.Headers.Add("Password", "abc");
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
StreamReader responseStream = new StreamReader(webresponse.GetResponseStream(), enc);
string result = string.Empty;
result = responseStream.ReadToEnd();
loResponseStream.Close();
webresponse.Close();
return result;
}
I want to call the same method in console application.
How can I do that?