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
"Server exception: not well-formed"
Its an Apache webserver running on Cent OS.
Below is the code. Could anyone help me with this. Thanks in advance.
Example of a POST link that server responds to correctly when tried from a browser:
https://some.server.com/some/local_dir/abc.cgi?command=ACommand&entitytype=AnEntity
My C# 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 = 开发者_JAVA技巧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&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);
}
Server responce:
12790618993000Server exception: not well-formed (invalid token): line 1, column 7
I'm not sure what is wrong with your format, but can I suggest doing the following:
- Download and install Fiddler - http://www.fiddler2.com/fiddler2/
- Download and install my Fiddler extension - Request to Code - http://www.chadsowald.com/software/fiddler-extension-request-to-code
- Run Fiddler.
- Configure Fiddler to decrypt HTTPS - Tools > Fiddler Options > HTTPS and tick the "Decrypt HTTPS Traffic" box.
- Visit your properly responding URL: https://some.server.com/some/local_dir/abc.cgi?command=ACommand&entitytype=AnEntity
- Drag that session row from the Fiddler session list into the 'Code' tab.
- It will generate C# code that you can use to make the same request.
Let me know if it's still not working if you try that.
精彩评论