HTTP Post XML document - server receives only first line

拈花ヽ惹草 提交于 2019-12-13 03:38:42

问题


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();

回答1:


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.




回答2:


It turns out the lines
loHttp.SendChunked = true;
loHttp.TransferEncoding = "7bit";

weren't needed.



来源:https://stackoverflow.com/questions/3992110/http-post-xml-document-server-receives-only-first-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!