How To Create A Php Client For A .net And Soap-based Web Service Api
Introduction
HttpClient form provides a base of operations class for sending/receiving the HTTP requests/responses from a URL. It is a supported async feature of .NET framework. HttpClient is able to process multiple concurrent requests. Information technology is a layer over HttpWebRequest and HttpWebResponse. All methods with HttpClient are asynchronous.
Example
In this case, I have created a console application.
To call Web API methods from the console Application, the first step is to install the required packages, using NuGet Package Manager. The following package needs to be installed in the console Application.
Install-Package Microsoft.AspNet.WebApi.Client
Side by side step is to create HttpClient object. In the following lawmaking snippet, the main function calls private static method "CallWebAPIAsync" and this blocks until CallWebAPIAsync completes the procedure, using wait function.
static void Main(string[] args) { CallWebAPIAsync() .Wait(); } static asyncTaskCallWebAPIAsync() { using(var client = newHttpClient()) { //Transport HTTP requests from here. } }
Afterwards, we accept set up the base URL for the HTTP request and set the Accept header. In this example, I accept set Accept header to "application/json" which tells the Server to send the information into JSON format.
using(var client = newHttpClient()) { client.BaseAddress = newUri("http://localhost:55587/"); customer.DefaultRequestHeaders.Accept.Clear(); customer.DefaultRequestHeaders.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json")); }
HTTP Get Asking
Following code is used to transport a Get request for section, as shown below:
using(var client = newHttpClient()) { client.BaseAddress = newUri("http://localhost:55587/"); client.DefaultRequestHeaders.Take.Clear(); client.DefaultRequestHeaders.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json")); //Get Method HttpResponseMessage response = awaitclient.GetAsync("api/Department/one"); if (response.IsSuccessStatusCode) { Departmentdepartment = awaitresponse.Content.ReadAsAsync < Section > (); Console.WriteLine("Id:{0}\tName:{1}", section.DepartmentId, section.DepartmentName); Console.WriteLine("No of Employee in Section: {0}", section.Employees.Count); } else { Console.WriteLine("Internal server Error"); } }
In the code given above, I have used GetAsync method to send HTTP Get request asynchronously. When the execution of this method is finished, it returns HttpResponseMessage, which contains HTTP response. If the response contains success code as response, information technology ways the response trunk contains the information in the course of JSON. ReadAsAsync method is used to deserialize the JSON object.
HttpClient does not throw whatever mistake when HTTP response contains an fault code, merely it sets the IsSuccessStatusCode property to false. If we want to treat HTTP error codes as exceptions, we can use HttpResponseMessage.EnsureSuccessStatusCode method.
When we run the code, given in a higher place, information technology throws the exception "Internal Server mistake".
We have to configure the serializer to discover then handle self-referencing loops. The post-obit code needs to be placed in Global.asax.cs in Web API:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings .PreserveReferencesHandling =Newtonsoft.Json.PreserveReferencesHandling.All;
HTTP POST request
Post-obit lawmaking is used to transport a Mail service asking for the department:
var section = newDepartment() { DepartmentName = "Test Department" }; HttpResponseMessage response = awaitclient.PostAsJsonAsync("api/Department", department); if (response.IsSuccessStatusCode) { // Go the URI of the created resource. UrireturnUrl = response.Headers.Location; Panel.WriteLine(returnUrl); }
In this code, PostAsJsonAsync method serializes the object into JSON format and sends this JSON object in Mail request. HttpClient has a built-in method "PostAsXmlAsync" to send XML in Postal service request. Also, we tin can use "PostAsync" for any other formatter.
HTTP PUT Asking
Following code is used to transport a PUT asking for the section:
//PUT Method var section = newDepartment() { DepartmentId = 9, DepartmentName = "Updated Department" }; HttpResponseMessage response = awaitclient.PutAsJsonAsync("api/Department", department); if (response.IsSuccessStatusCode) { Console.WriteLine("Success"); }
Same equally Post, HttpClient also supports all three methods: PutAsJsonAsync, PutAsXmlAsync and PutAsync.
HTTP DELETE Asking
Following code is used to send a DELETE request for the department:
intdepartmentId = nine; HttpResponseMessage response = awaitclient.DeleteAsync("api/Department/" + departmentId); if (response.IsSuccessStatusCode) { Console.WriteLine("Success"); }
HttpClient only supports DeleteAsync method because Delete method does non have a request torso.
Summary
HttpClient is used to send an HTTP request, using a URL. HttpClient can be used to brand Web API requests from the console Application, Winform Application, Web course Application, Windows shop Application, etc.
How To Create A Php Client For A .net And Soap-based Web Service Api,
Source: https://www.c-sharpcorner.com/article/calling-web-api-using-httpclient/
Posted by: matthiesaltrove88.blogspot.com
0 Response to "How To Create A Php Client For A .net And Soap-based Web Service Api"
Post a Comment