Consume Web API data using HttpClient
in c#
HttpClient
is a base class for sending HTTP requests and receiving HTTP responses from a
resource identified by a URI.
Following
code example get movies data in JSON format,
Web API code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Threading;
using System.Net.Http.Headers;
public class RestAPICall
{
private const string URL = "https://c4utest.com/api/movies/search";
private const string urlParameters = "?Title=Bahubali ";
public void DisplayMoviesFromRestAPI()
{
using (var client = new
HttpClient())
{
client.BaseAddress = new
Uri(URL);
// List data response.
HttpResponseMessage response =
client.GetAsync(urlParameters).Result;
// blocking call
if
(response.IsSuccessStatusCode)
{
// Parse the
response body.
var dataObjects =
response.Content.ReadAsStringAsync().Result;
//data objects
Movies deserializedProduct
= JsonConvert.DeserializeObject<Movies>(dataObjects);
JObject jsonval =
JObject.Parse(dataObjects);
if (deserializedProduct !=
null)
{
Console.WriteLine("Total movies : "+
deserializedProduct.total);
Console.WriteLine("Current page : " +
deserializedProduct.page);
Console.WriteLine("Total pages : " +
deserializedProduct.total_pages);
//first movie details
var firstMoviesDetails
= deserializedProduct.data.FirstOrDefault();
//Display first moview
details from collections
Console.WriteLine("First movie title : " +
firstMoviesDetails.Title);
Console.WriteLine("First movie release
year : " + firstMoviesDetails.Year);
Console.WriteLine("First movie code : " +
firstMoviesDetails.imdbID);
//display all movies
details
foreach (var
movieDetail in deserializedProduct.data)
{
Console.WriteLine("Movie title : " + movieDetail.Title);
Console.WriteLine("Movie release year : " + movieDetail.Year);
Console.WriteLine("Movie
code : " + movieDetail.imdbID);
}
}
}
else
{
Console.WriteLine("{0}
({1})", (int)response.StatusCode, response.ReasonPhrase);
}
}
}
}
Movies class
public class Movies
{
public int page { get; set; }
public int per_page { get; set; }
public int total { get; set; }
public int total_pages { get; set; }
public List<MoviesData> data { get; set; }
}
MoviesData class
public class MoviesData
{
public string Title { get; set; }
public int Year { get; set; }
public string imdbID { get; set; }
}
What is anonymous
function in C#?
An anonymous function is an
"inline" statement or expression that can be used wherever a delegate
type is expected.
You can use it to initialize a named
delegate or pass it instead of a named delegate type as a method parameter.
You can use a lambda expression or an
anonymous method to create an anonymous function. We recommend using lambda
expressions as they provide more concise and expressive way to write inline
code.
Reference…https://docs.microsoft.com/
How to use anonymous
function in C#?
Anonymous function code examples using,
- Delegate with named method,
- Lambda expression
- Delegate with anonymous method
- Built-in delegates (Func & Action
delegates)
delegate void SampleDelegate(string x);
public class
AnonymousFunctions
{
static void
DisplayData(string x)
{
Console.WriteLine("Data is :" +
x);
}
static void
AnonymousSamples()
{
//
initialization with a named method.
SampleDelegate testNamed = new SampleDelegate(DisplayData);
// C#
2.0: A delegate can be initialized with
// inline
code, called an "anonymous method." This
//
method takes a string as an input parameter.
SampleDelegate TestAnMethod = delegate (string s) {
Console.WriteLine(s); };
// C#
3.0. A delegate initialization with a lambda expression.
//
The lambda also takes a string
// as
an input parameter (x). The type of x is inferred by the compiler.
SampleDelegate TestLambdaExp = (x)
=> { Console.WriteLine(x); };
//Anonymous
function with action generic delegate
Action<string> TestDelGeneric = (s)
=> { Console.WriteLine(s); };
//
Anonymous function with action delegate without paremeters.
Action TestDelActGen = () => {
Console.WriteLine("Codechef4U
samples"); };
//Anonymous
Function with func generic delegate, first 2
//
parameters are input, and last parameter is result
Func<string, string, string> GetCombinedString =
(x, y) => { return x+ " " + y; };
//Anonymous
Function with predicate generic delegate
Predicate<string> isValidSurName = (x)
=> { return x != null && x.Length >2 ? true : false;
};
//
Invoke the delegates.
DisplayData("The named method call using delegate .");
TestAnMethod("The anonymous method call.");
TestLambdaExp("The lambda call.");
TestDelGeneric("The action generic delegate .");
TestDelActGen();
Console.WriteLine("Full name is: "+
GetCombinedString("Aditya", "Kendre"));
Console.WriteLine("Is valid name :" +
isValidSurName("Kendre"));
}
}
CSS
Cascading Style Sheets (CSS) is a simple design language released
in 1996, CSS describes how HTML elements are to be displayed on screen, paper,
or in other media.
CSS 3
CSS3 version introduced new features that allowed
developers to style HTML elements with less CSS code.
CSS3 features
Easy to use
CSS3 allows developers to style HTML elements easier.
Responsive design
CSS3 is the latest version, hence it supports responsive
design.
Box Shadow
One of the CSS3 new features is the box-shadow property
that adds a shadow to an element. Instead of using multiple images around an
item, this property lets you add shadow with a short line of code.
Separate reusable modules
CSS3 can be breakdown into modules.
Attribute Selectors
CSS3 also introduced new selectors in addition to the ones
in CSS2. Instead of applying IDs or classes for styling, developers can select
HTML elements according to their attributes.
Performance and speed
CSS3 is faster than basic CSS.
Animation
Animation and 3D transformations are supported
Compatibility
CSS3 is backwards-compatible with former CSS versions. It
means that CSS3 features work on web pages using older CSS. Browsers that
support CSS2 also represent the modifications with CSS3..
Color
CSS3 supports HSL RGBA, HSLA and the gradient colors. i.e.
#fff supported.
Rounded corners
CSS3 provide codes for setting rounded gradients and
corners
Text shadow and effects
CSS3 has many advance features like text shadows, visual
effects, and a wide range of font style and color.
Opacity
One of the CSS3 properties called opacity makes elements
see-through or completely transparent.