C# HTTPS Post. Web Server Throwing "Not Well formed" Exception
<p>Im attempting a Https post from a C# application. I could connect to the web server and authinticate the user. The server is configured to send through some data in responce to a POST query. If tried from a browser the server does repond properly. However, when I attempt the same query from my application, the server responds with an error saying </p>

<p>"Server exception: not well-formed"</p>

<p>Its an Apache webserver running on Cent OS. </p>

<p>Below is the code. Could anyone help me with this. Thanks in advance. </p>

<hr>

<p>Example of a POST link that server responds to correctly when tried from a browser:</p>

<p><a href="https://some.server.com/some/local_dir/abc.cgi?command=ACommand&amp;entitytype=AnEntity" rel="nofollow">https://some.server.com/some/local_dir/abc.cgi?command=ACommand&amp;entitytype=AnEntity</a></p>

<hr>

<p>My C# code:</p>

<pre><code>ServicePointManager.ServerCertificateValidationCallback = delegate
{ return true; };

NetworkCredential nc = new NetworkCredential("User", "Password");
CredentialCache cc = new CredentialCache();
cc.Add("https://some.server.com", 443, "Basic", nc);

HttpWebRequest WebRequest =
(HttpWebRequest)System.Net.WebRequest.Create("https://some.server.com/some/local_dir/abc.cgi");

WebRequest.KeepAlive = true;
WebRequest.Method = "POST";
WebRequest.AllowAutoRedirect = false;
WebRequest.Credentials = cc;
WebRequest.PreAuthenticate = true;
WebRequest.ContentType = "application/x-www-form-urlencoded";
WebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; " +
        "Windows NT 5.1; SV1; .NET CLR 1.1.4322; " +
        ".NET CLR 2.0.50215)";

byte[] AuthBytes = Encoding.ASCII.GetBytes("User:Password");
string sAuth = Convert.ToBase64String(AuthBytes);
WebRequest.Headers.Add("Authorization", "Basic " + sAuth);


byte[] bytes = Encoding.UTF8.GetBytes("command=ACommand&amp;entitytype=AnEntity");
using (Stream requestStream = WebRequest.GetRequestStream())
{
    requestStream.Write(bytes, 0, bytes.Length);
}

try
{

    WebResponse response = WebRequest.GetResponse();
    Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd());

}
catch (Exception ex)
{
    Console.WriteLine("Exception: "+ex.Message);
} 
</code></pre>

<hr>

<p>Server responce: </p>

<p>12790618993000Server exception: not well-formed (invalid token): line 1, column 7</p>
