Reputation: 2295
I want to make a get request to the following end point through C#.
http://api.example.com/1/companies/{id}
I was looking at WCF rest starter kit and wonder there are other ways to make get requests easily than installing rest starter kit.
Please suggest. Thanks,
Upvotes: 2
Views: 6264
Reputation: 7013
Here's a method that will execute GET from URL and return string:
public static string GetResponse(string endPoint)
{
HttpWebRequest request = CreateWebRequest(endPoint);
using (var response = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK)
{
string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
}
// grab the response
using (var responseStream = response.GetResponseStream())
{
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
}
EDIT:
private static HttpWebRequest CreateWebRequest(string endPoint)
{
var request = (HttpWebRequest)WebRequest.Create(endPoint);
request.Method = "GET";
request.ContentLength = 0;
request.ContentType = "text/json";
return request;
}
Upvotes: 3
Reputation: 1752
Create a new Class Library Project and call it “RESTService.Lib”. Add references to “System.ServiceModel” and “System.ServiceModel.Web”. Create an Interface class called IRESTDemoServices and add the definitions of the methods that represent the services offered. Our interface will offer just one service as follows:
public interface IRESTDemoServices
{
string GetClientNameById(string Id);
}
In order to tell the framework to treat this interface as a service you need to decorate it as follows:
[ServiceContract(Name = "RESTDemoServices")]
public interface IRESTDemoServices
{
[OperationContract]
string GetClientNameById(int Id);
}
Define the URL to be used to access the service (URL Routing):
public static class Routing
{
public const string GetClientRoute = "/Client/{id}";
}
The connection of the URL Route to the method in the interface is achieved by decorating the interface with an attribute as follows:
[ServiceContract(Name = "RESTDemoServices")]
public interface IRESTDemoServices
{
[OperationContract]
[WebGet(UriTemplate = Routing.GetClientRoute, BodyStyle = WebMessageBodyStyle.Bare)]
string GetClientNameById(string Id);
}
Now implement the service:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RestDemoServices:IRESTDemoServices
{
public string GetClientNameById(string Id)
{
Random r = new Random();
string ReturnString="";
int Idnum=Convert.ToInt32(id);
for (int i = 0; i < Idnum; i++)
ReturnString += char.ConvertFromUtf32(r.Next(65, 85));
return ReturnString;
}
}
And then you have to host it either in IIS or by using custom hosting (console, windows service, etc..).
Look here: http://www.progware.org/blog/post/a-simple-rest-service-in-c.aspx
Hope that helps !
Upvotes: 1