Today we will understand how to consume the WCF REST services on the client side.
The theory of REST can be understood from numerous resources online. This post will explain how to make the different types of requests.
Why REST?
Anyone who has worked with web-services and WCF is often confused as to why one should create RESTful services when SOAP has been easy and good to use for so long. So the short answer for using REST is less data traffic on the network. Every data packet to be sent over SOAP is packaged in a bulky header.
The main verbs in a REST implementation are GET, PUT, POST. There are other verbs as well but they are infrequently used. One can try these requests by using a popular tool like WebFetch (we will discuss using the WebFetch tool in a later post)
Lets go to some practical examples:
In many applications viz the secured ones, every client is issued a certificate so that every REST request from the client is authenticated and then authorised to perform the operation (specified by the verb)
How to use the client certificates in REST:
HttpClient client = new HttpClient( <Url on which request is to be made> );
string certLoc = <certificate location>;
string certPwd = <certificate pwd>;
X509Certificate2 cert = new X509Certificate2(certLoc, certPwd);
client.TransportSettings.ClientCertificates.Add(cert);
1) The complete sample for GET request would look as below,
using (HttpClient client = new HttpClient( <Url on which request is to be made> ))
{
// Initalise a response object
HttpResponseMessage response = null;
string certLoc = <certificate location>;
string certPwd = <certificate pwd>;
X509Certificate2 cert = new X509Certificate2(certLoc, certPwd);
client.TransportSettings.ClientCertificates.Add(cert);
string sRestAddress = <Url on which request is to be made>;
client.BaseAddress = new Uri(sRestAddress);
// encoding to be used. base64_UTF16_Encode is a user-defined function
string sBase64EncodedMembershipNumber = base64_UTF16_Encode(sMembershipNumber);
string authrorization = "Basic" + sBase64EncodedMembershipNumber + "\r\n";
client.DefaultHeaders.Authorization = new Microsoft.Http.Headers.Credential(authrorization);
client.DefaultHeaders.Accept.Add("text/xml\r\n");
string requestPath = <request path of the resource>;
// Make the request and retrieve the response
response = client.Get(sRestAddress + requestPath);
response.Content.LoadIntoBuffer();
return response.Content.ReadAsByteArray();
}
The above code snippet shows how we can make a GET request and pass the client certificate and logging credentials along with the request. Response could either be in xml or pdf format and can be specified in the header by using the Accept type in the HTTP request.
2) The PUT request would look like below,
using (HttpClient client = new HttpClient( <Url on which request is to be made> ))
{
// Initalise a response object
HttpResponseMessage response = null;
string certLoc = <certificate location>;
string certPwd = <certificate pwd>;
X509Certificate2 cert = new X509Certificate2(certLoc, certPwd);
client.TransportSettings.ClientCertificates.Add(cert);
string sRestAddress = <Url on which request is to be made>;
client.BaseAddress = new Uri(sRestAddress);
// encoding to be used. base64_UTF16_Encode is a user-defined function
string sBase64EncodedMembershipNumber = base64_UTF16_Encode(sMembershipNumber);
string authrorization = "Basic" + sBase64EncodedMembershipNumber + "\r\n";
client.DefaultHeaders.Authorization = new Microsoft.Http.Headers.Credential(authrorization);
client.DefaultHeaders.Accept.Add("text/xml\r\n");
string requestPath = <request path of the resource>;
// XML file which contains the data to be sent
XDocument doc = XDocument.Load(@"C:\Personal\Test.xml");
HttpContent body = HttpContent.Create(doc.ToString(SaveOptions.DisableFormatting), Encoding.UTF8, "text/xml");
response = client.Put(sRestAddress + requestPath, body);
response.Content.LoadIntoBuffer();
return response.Content.ReadAsByteArray();
}
The above code snippet shows how to send a PUT request to the server. PUT/POST requests are used to send data to the server. The exact type of verb to be used is decided by the author of the Server code and the requirement whether the state of server is to be changed or not.
REST is fairly easy and simple to use.
The theory of REST can be understood from numerous resources online. This post will explain how to make the different types of requests.
Why REST?
Anyone who has worked with web-services and WCF is often confused as to why one should create RESTful services when SOAP has been easy and good to use for so long. So the short answer for using REST is less data traffic on the network. Every data packet to be sent over SOAP is packaged in a bulky header.
The main verbs in a REST implementation are GET, PUT, POST. There are other verbs as well but they are infrequently used. One can try these requests by using a popular tool like WebFetch (we will discuss using the WebFetch tool in a later post)
Lets go to some practical examples:
In many applications viz the secured ones, every client is issued a certificate so that every REST request from the client is authenticated and then authorised to perform the operation (specified by the verb)
How to use the client certificates in REST:
HttpClient client = new HttpClient( <Url on which request is to be made> );
string certLoc = <certificate location>;
string certPwd = <certificate pwd>;
X509Certificate2 cert = new X509Certificate2(certLoc, certPwd);
client.TransportSettings.ClientCertificates.Add(cert);
1) The complete sample for GET request would look as below,
using (HttpClient client = new HttpClient( <Url on which request is to be made> ))
{
// Initalise a response object
HttpResponseMessage response = null;
string certLoc = <certificate location>;
string certPwd = <certificate pwd>;
X509Certificate2 cert = new X509Certificate2(certLoc, certPwd);
client.TransportSettings.ClientCertificates.Add(cert);
string sRestAddress = <Url on which request is to be made>;
client.BaseAddress = new Uri(sRestAddress);
// encoding to be used. base64_UTF16_Encode is a user-defined function
string sBase64EncodedMembershipNumber = base64_UTF16_Encode(sMembershipNumber);
string authrorization = "Basic" + sBase64EncodedMembershipNumber + "\r\n";
client.DefaultHeaders.Authorization = new Microsoft.Http.Headers.Credential(authrorization);
client.DefaultHeaders.Accept.Add("text/xml\r\n");
string requestPath = <request path of the resource>;
// Make the request and retrieve the response
response = client.Get(sRestAddress + requestPath);
response.Content.LoadIntoBuffer();
return response.Content.ReadAsByteArray();
}
The above code snippet shows how we can make a GET request and pass the client certificate and logging credentials along with the request. Response could either be in xml or pdf format and can be specified in the header by using the Accept type in the HTTP request.
2) The PUT request would look like below,
using (HttpClient client = new HttpClient( <Url on which request is to be made> ))
{
// Initalise a response object
HttpResponseMessage response = null;
string certLoc = <certificate location>;
string certPwd = <certificate pwd>;
X509Certificate2 cert = new X509Certificate2(certLoc, certPwd);
client.TransportSettings.ClientCertificates.Add(cert);
string sRestAddress = <Url on which request is to be made>;
client.BaseAddress = new Uri(sRestAddress);
// encoding to be used. base64_UTF16_Encode is a user-defined function
string sBase64EncodedMembershipNumber = base64_UTF16_Encode(sMembershipNumber);
string authrorization = "Basic" + sBase64EncodedMembershipNumber + "\r\n";
client.DefaultHeaders.Authorization = new Microsoft.Http.Headers.Credential(authrorization);
client.DefaultHeaders.Accept.Add("text/xml\r\n");
string requestPath = <request path of the resource>;
// XML file which contains the data to be sent
XDocument doc = XDocument.Load(@"C:\Personal\Test.xml");
HttpContent body = HttpContent.Create(doc.ToString(SaveOptions.DisableFormatting), Encoding.UTF8, "text/xml");
response = client.Put(sRestAddress + requestPath, body);
response.Content.LoadIntoBuffer();
return response.Content.ReadAsByteArray();
}
The above code snippet shows how to send a PUT request to the server. PUT/POST requests are used to send data to the server. The exact type of verb to be used is decided by the author of the Server code and the requirement whether the state of server is to be changed or not.
REST is fairly easy and simple to use.