How to call a REST API from a console application?
The answer of my REST service will be in XML format.
On the net, I'm 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 a console application.
How can i do this?
source
share