There is plenty of articles out there on how to make a ajax call to a asp.net web api service. but what if you want to make a call to the same service from a server code? this can happen because of various reasons like when you want to offer a service and use IP validation.
anyway this is quite simple to call a service exposed by ASP.net WEB API either using JSON or XML. and it is like calling a WCF or XML web service from a server side code without adding a reference to the service.
Lets assume that our ASP.net WEB API provide a service to and and return flights using a given FlightSearchRequest object.
FlightSearchRequest
1: public class FlightSearchRequest
2: {
3: public string DepartureAirportCode { get; set; }
4:
5: public string ArrivalAirportCode { get; set; }
6: }
WEB API Method
1: [HttpPost]
2: public IEnumerable<Flight> GetFlights(FlightSearchRequest flightRequest)
3: {
4:
5: return new List<Flight>
6: {
7: new Flight { Id=1, AirlineCode = "EK", Price = 350 },
8: new Flight { Id=2, AirlineCode = "LX", Price = 280 },
9: };
10: }
and here is the code to call the service:
Using JSON
1: HttpWebRequest request;
2: HttpWebResponse hwResponse;
3: string url = "http://127.0.0.1:26030/api/MyAPI/GetFlights";
4: string responseBody = string.Empty;
5: string requestBody =
"[{\"DepartureAirportCode\":\"MEL\",\"ArrivalAirportCode\":\"LHR\"}]";
6:
7: byte[] byteData = UTF8Encoding.UTF8.GetBytes(requestBody.ToString());
8: request = (HttpWebRequest)HttpWebRequest.Create(url);
9: request.ContentType = "application/json;charset=utf-8";
10: request.UserAgent =
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; BOIE9;ENUS)";
11: request.Accept = "*/*";
12: request.Method = "POST";
13: request.ContentLength = byteData.Length;
14:
15: // Write data
16: using (Stream postStream = request.GetRequestStream())
17: {
18: postStream.Write(byteData, 0, byteData.Length);
19: }
20:
21: // Get response
22: using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
23: {
24: // Get the response stream
25: using (var reader = new StreamReader(response.GetResponseStream()))
26: {
27: responseBody = reader.ReadToEnd();
28: }
29: }
to make the same call using XML you just need to change the ContentType to application/xml and also change the request body to an xml format
Using XML
1: HttpWebRequest request;
2: HttpWebResponse hwResponse;
3: string url = "http://127.0.0.1:26030/api/MyAPI/GetFlights";
4: string responseBody = string.Empty;
5: string requestBody =
"<Product><Id>6</Id><Name>Jackson</Name></Product>";
6: byte[] byteData = UTF8Encoding.UTF8.GetBytes(requestBody.ToString());
7: request = (HttpWebRequest)HttpWebRequest.Create(url);
8: request.ContentType = "application/xml;charset=utf-8";
9: request.UserAgent =
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; BOIE9;ENUS)";
10: request.Accept = "*/*";
11: request.Method = "POST";
12: request.ContentLength = byteData.Length;
13:
14: // Write data
15: using (Stream postStream = request.GetRequestStream())
16: {
17: postStream.Write(byteData, 0, byteData.Length);
18: }
19:
20: // Get response
21: using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
22: {
23: // Get the response stream
24: using (var reader = new StreamReader(response.GetResponseStream()))
25: {
26: responseBody = reader.ReadToEnd();
27: }
28: }
now the more I play with WEP API the more I like it