Tuesday, November 16, 2021

Calling a web service URL in console application ASP.NET C# or WEB API

These are the three methods we can use for calling web service:-

static void Main(string[] args)
        {         
            string url = string.Format("http://localhost:85/WebService.asmx/HelloWorld?id=1&id1=8");

            //HttpClient

            using (var client = new WebClient())
            {
                client.Headers.Add("Content-Type:application/xml");
                client.Headers.Add("Accept:application/xml");

                var result = client.DownloadString(url);
                Console.WriteLine(Environment.NewLine + result);
            }
}
--------------------------------******************-------------------------

static void Main(string[] args)
        {         
            string url = string.Format("http://localhost:85/WebService.asmx/HelloWorld?id=1&id1=8");

          WebRequest request = HttpWebRequest.Create(url);
            
            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string urlText = reader.ReadToEnd();
                    //Do whatever you need to do
                    Console.WriteLine(urlText);
                }
            }
}
-------------------------------******************----------------------------

static void Main(string[] args)
        {         
            string url = string.Format("http://localhost:85/WebService.asmx/HelloWorld?id=1&id1=8");

         HttpWebRequest request1 = WebRequest.Create(url) as HttpWebRequest;
            //request.Accept = "application/xrds+xml";  
            HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
            WebHeaderCollection header = response1.Headers;

            var encoding = ASCIIEncoding.ASCII;
            using (var reader = new System.IO.StreamReader(response1.GetResponseStream(), encoding))
            {
                string responseText = reader.ReadToEnd();
                Console.WriteLine(responseText);
            }
}


-------------------**********************------------------------------------
but I will prefer (best method)

static void Main(string[] args)
        {         
            string url = string.Format("http://localhost:85/WebService.asmx/HelloWorld?id=1&id1=8");
        // demo data from website
        //string url = string.Format("https://reqres.in/api/users?page=2");

          WebRequest request = HttpWebRequest.Create(url);
            
            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string urlText = reader.ReadToEnd();
                    //Do whatever you need to do
                    Console.WriteLine(urlText);
                }
            }
}



No comments:

Post a Comment