Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# consuming post rest service

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers; 

namespace ConsoleProgram
{
    public class DataObject
    {
        public string Name { get; set; }
    }

    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();
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: unity overlapsphere 
Csharp :: order by length descending C# 
Csharp :: inline creation dictionnary C# 
Csharp :: how to reference a UI element in unity 
Csharp :: prevent asp button from postback 
Csharp :: unity get perlin noise 3d 
Csharp :: gameobject on click unity 
Csharp :: flip a character in unity 
Csharp :: how to make colliders collide with some things but not other in unity 
Csharp :: function on animation exit unity 
Csharp :: c# add multiple items to list 
Csharp :: unity 3d camera movement script 
Csharp :: how to use navmeshagent in unity 
Csharp :: generate entity model dot net core 
Csharp :: c# list audio devices 
Csharp :: instantiate object in circle 
Csharp :: Task.FromResult(null) 
Csharp :: fluent assertion exception 
Csharp :: float and int need help 
Csharp :: wpf color picker 
Csharp :: c# get foreground window 
Csharp :: HCF of list of number 
Csharp :: top level statements c# 
Csharp :: arcane 
Csharp :: remove duplicate characters in a string c# 
Csharp :: get array from column datatable c# 
Csharp :: c# get list item in random order 
Csharp :: c# alphabetize a list of string 
Csharp :: how to get current dir in c# 
Csharp :: how to concatenate two arrays in c# 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =