Consuming HTTP Response from a web site inside Windows Desktop app

Please point me to what I should look at (libraries, classes etc). I have a Windows Froms desktop application and was asked to write a proof of concept tool for calling a web page and getting the response from it.

So say I call http://www.example.com/getdetails.html?id=7

And I will receive the details of the object with id=7, whatever the page returns. Which class/library should I be looking at? Thank you

PS: please don't give me examples pertaining to APIs or SOAP or REST web services. I am not calling those. Thank you!

2 answers

  • answered 2019-06-11 22:57 Mariano Luis Villa

    You could use HttpWebRequest. Here's an example:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.getdata.com/getdetails.html?id=7");
        request.Method = "GET";
    

    Then, you would get the response like this:

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
              //Use the response. 
        }
    

    EDIT.

    As per Erik's comment, HttpWebRequest is quite old (which isn't necessarily bad). I use it a lot because it grants more control, which fits my usual needs, but HttpClient and WebClient are both more friendly and will do the job in most cases.

  • answered 2019-06-11 23:16 Mohammed AZHAR UDDIN

    First thing is to understand do you want to call web page or web service, because web pages are html with content and web services just return Content which is more useful.

    If you want to call web page please use

       using (var client = new WebClient())
        {
            var contents = client.DownloadString("http://www.getdata.com/getdetails.html?id=7");
            Console.WriteLine(contents);
        }
    

    you can call external services in multiple ways, and there are 2 types of web services 1) SOAP 2) REST

    Consuming SOAP services is a bit lengthy process and REST services are easy to access

    To access REST API you can use below code

        public class Class1
        {
        private const string URL = "https://sub.domain.com/objects.json";
    
        private string urlParameters = "?api_key=123";
    
        static void Main(string[] args)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(URL);
    
            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
    
            // List data response.
            HttpResponseMessage response = client.GetAsync(urlParameters).Result;  // Blocking call! Program will wait here until a response is received or a timeout occurs.
            if (response.IsSuccessStatusCode)
            {
                // Parse the response body.
                var dataObjects = response.Content.ReadAsAsync<IEnumerable<DataObject>>().Result;  //Make sure to add a reference to System.Net.Http.Formatting.dll
                foreach (var d in dataObjects)
                {
                    Console.WriteLine("{0}", d.Name);
                }
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
    
            //Make any other calls using HttpClient here.
    
            //Dispose once all HttpClient calls are complete. This is not necessary if the containing object will be disposed of; for example in this case the HttpClient instance will be disposed automatically when the application terminates so the following call is superfluous.
            client.Dispose();
        }
    }
    

    and to call SOAP service , adding a link of a page which explains in detail as it is a lengthy process

    https://www.c-sharpcorner.com/UploadFile/0c1bb2/consuming-web-service-in-Asp-Net-web-application/