tags:

views:

27

answers:

2

I have C# application that sends an XML document to a server via HTTPS Post. The problem is that the server receives only the first line <?xml version="1.0" encoding="UTF-8"?>. Here is a truncated version of my code (important parts only). What could be causing this problem? Is there modify in my code?

SSL connectivity to the server has been assured, and the message I recevie in return is "document type not accepted".

thanks!

 StreamWriter loPostData = null;
 HttpWebRequest loHttp = null;
 HttpWebResponse loWebResponse = null;
 byte[] buffer;

 String uri = ConfigurationSettings.AppSettings["URL"];

 loHttp = (HttpWebRequest)WebRequest.Create(uri);
 buffer = Encoding.ASCII.GetBytes(payload);

 //Request Header
 loHttp.ProtocolVersion = HttpVersion.Version11;
 loHttp.KeepAlive = true;
 loHttp.Accept = "text/xml;charset=\"utf-8\"";
 loHttp.Method = WebRequestMethods.Http.Post;
 loHttp.ContentType = "text/xml;charset=\"utf-8\"";
 loHttp.ContentLength = buffer.Length;
 loHttp.SendChunked = true;
 loHttp.TransferEncoding = "7bit";
 loHttp.AllowWriteStreamBuffering = true;

 ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
   {
       return true; // **** Always accept return
   };

   X509Certificate x509_1 = new X509Certificate(ConfigurationSettings.AppSettings["OPEN_INVOICE_CERTIFICATE"]);
   loHttp.ClientCertificates.Add(x509_1);

    //Send data
   loPostData = loHttp.GetRequestStream();
   loPostData.Write(buffer, 0, buffer.Length);
   loPostData.Close();

    //Get a response
    loWebResponse = (HttpWebResponse)loHttp.GetResponse();
    StreamReader responsestream = new StreamReader(loWebResponse.GetResponseStream());
    String rsp = responsestream.ReadToEnd();

    responsestream.Close();
A: 

Based on the error message you are getting, are you sure that the page you are calling REALLY wants the content type to be text/xml? It might be that you need to post it like a standard webrequest instead of posting an HTML file directly by content type.

Mitchel Sellers
Yep, this is infact what the provider said. I can test it out omitting that.
marcwenger
I confirmed that the server requires "text/xml;charset=utf-8", otherwise it rejects the payload completely. With the aforementioned string included, it accepts the message, but only the first line.
marcwenger
What is the code that you are using to load the document?
Mitchel Sellers
The document gets passed in to this method as a `string`. It something along the lines of this example: http://forums.techarena.in/software-development/1195703.htm. After it loads the string into the byte[] object.
marcwenger
Here's more: I sent out to the server (which is a third-party server) a non-xml string. I just encoded a few characters, and I got back the same response. It might tell me that I'm not correctly sending the document. After doing some more testing, I found out the in the stream objects the Length and Position properties are not supported. Would this cause an error or conflict on the receiving side?
marcwenger