Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am exploring RestFul Web API. I dont know how to implement it. so need some help..share some article if you know.
Posted
Updated 27-May-15 22:41pm
v3

Hi,
Try this REST web API example.
HTTP GET Requests
C#
static string HttpGet(string url) {
  HttpWebRequest req = WebRequest.Create(url)
                       as HttpWebRequest;
  string result = null;
  using (HttpWebResponse resp = req.GetResponse()
                                as HttpWebResponse)
  {
    StreamReader reader =
        new StreamReader(resp.GetResponseStream());
    result = reader.ReadToEnd();
  }
  return result;
}


HTTP POST Requests
C#
<pre lang="c#">

static string HttpPost(string url,
string[] paramName, string[] paramVal)
{
HttpWebRequest req = WebRequest.Create(new Uri(url))
as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";

StringBuilder paramz = new StringBuilder();
for (int i = 0; i < paramName.Length; i++) {
paramz.Append(paramName[i]);
paramz.Append("=");
paramz.Append(HttpUtility.UrlEncode(paramVal[i]));
paramz.Append("&");
}

// Encode the parameters as form data:
byte[] formData =
UTF8Encoding.UTF8.GetBytes(paramz.ToString());
req.ContentLength = formData.Length;

// Send the request:
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}


string result = null;
using (HttpWebResponse resp = req.GetResponse()
as HttpWebResponse)
{
StreamReader reader =
new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}

return result;
}
 
Share this answer
 
Comments
Member 9579525 29-May-15 0:46am    
Thank u..I want to implement Restfull API which will work on all platforms e.g web,mobile,tab etc. above links expailned it in mvc only. so how to do Restful websetrvices so that it will work on all platforms.
Shivachalappa Gotur 29-May-15 7:12am    
Hi,
Please see if it helps you.
Calling Web API from a Windows Phone 8 Application (C#):-

http://www.asp.net/web-api/overview/mobile-clients/calling-web-api-from-a-windows-phone-8-application

How to Design REST APIs for Mobile?:-
http://www.redotheweb.com/2012/08/09/how-to-design-rest-apis-for-mobile.html
Member 9579525 1-Jun-15 6:08am    
Thank you.
After creating Web Api, i want to call it from my application so how to call it? and can i call one web API for different platforms?
Shivachalappa Gotur 4-Jun-15 7:44am    
Hi,
Yes you can call one web API for different platforms. Please have a look at example, .Net web API service with Java Client.
http://www.codeproject.com/Articles/827669/NET-Web-API-Service-with-a-Java-Client

Calling Web API From Application:
1.How to create web API.
http://www.c-sharpcorner.com/UploadFile/a6fd36/understand-self-host-of-a-web-apiC-Sharp/
2. How to Call Web API after creating it.
http://www.c-sharpcorner.com/uploadfile/a6fd36/understanding-how-to-call-the-web-api-from-a-client-applica/

Sincerely
Shivachalappa K Gotur
https://in.linkedin.com/pub/shivachalappa-gotur/25/907/b37

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900