Consuming a RESTful Web Service Using HTTP POST from .NET
I recently had the need to consume a RESTful web service with .NET. What made this less typical than most articles I found on the internet is that instead of retrieving data with the web service via an HTTP GET, I needed to send data with the web service via an HTTP POST. I didn't see anyone doing that.
So this blog post is here for three purposes. First, it is here to help others needing to call any RESTful web service from .NET using an HTTP POST. Second, it is here so that next time I need to do it, I can find it quickly. Third, anyone else needing to consume the specific Taxcient web service that I was consuming may find this a helpful starting point.
The web service I needed to call is hosted by a company named Taxcient. Their web service allows me to pass customer data to it using an HTTP POST. Here is the basic code I used:
using System.Net;
// The following List contains each record I am passing to the web service.
List records = GetData();
// Create a single string containing all the records.
StringBuilder data = new StringBuilder();
foreach (string record in records)
{
data.AppendLine(record);
}
WebClient request = new WebClient();
// Add the required headers.
request.Headers.Add("Authorization",
string.Format("Basic {0}", GetAuthenticationCredentials()));
request.Headers.Add("Content-Type",
TaxcientFileSettings.TaxcientInterfaceSettings.ContentType);
try
{
string results = request.UploadString(url, "POST", data.ToString());
using (StringReader reader = new StringReader(results))
{
string result;
// Read off the mode line. This is Taxcient specific.
result = reader.ReadLine();
if (result != null)
{
// Read off the header line. This is Taxcient specific.
result = reader.ReadLine();
if (result != null)
{
while ((result = reader.ReadLine()) != null)
{
// Handle each result. Check for errors, log, etc.
}
}
}
}
}
catch (Exception ex)
{
// Handle the exception.
}
I have removed all the error handling, logging, and data-specific logic.
I found that using the WebClient class worked best for my needs. I tried some other .NET classes such as WebRequest/HttpWebRequest but couldn't get them to work for my needs. Since I needed to pass a string of data to the WebClient UploadString method, I needed to build a string of every line in my list. So I created a StringBuilder and looped through each item in the list appending it to the StringBuilder.
Next I instantiated my WebClient object. The Taxcient web service required me to pass the authorization credentials and content type as headers to the HTTP POST, so I added them next.
I then called the WebClient's UploadString method which is where the web service call happens. I get back a string of results that I then read through using a StringReader. The Taxcient web service actually returns two lines prior to the actual results, a mode line and a header line. This is Taxcient specific and you probably won't have to do this. After I read off the mode and header lines, I then loop through all the remaining lines. Each of those lines is a success/error message corresponding to each record from my original list.
That's all it took. Not much code at all really. And, if I didn't have to read off the mode and header lines, and handle them separately, it would have been several lines shorter still.