I keep getting one of the following error messages :
\"The remote server returned an error: (400) Bad Request.\"
OR
\"System.Net.ProtocolVio
A couple things that are "off":
Write directly to your writer There shouldn't be a reason to call GetBytes(). The StreamWriter is perfectly capable of writing a string to the stream:
writer.Write(bld.ToString());
Use the using() {} pattern around your StreamWriter
This will ensure proper disposal of the writer object.
using(var writer = new StreamWriter(req.GetRequestStream()))
{
writer.Write(bld.ToString());
}
You don't need to explicitly set the content length Leave it alone, the framework will set it for you based on what you write to the request stream.
If you need to be explicit about using ASCII, set the charset in the Content-Type header
req.ContentType = "application/x-www-form-urlencoded; charset=ASCII";
You should also specify the encoding when instantiating your StreamWriter:
new StreamWriter(req.GetRequestStream(), Encoding.ASCII)