HTTP发布C#中的XML数据

我需要HTTP将XML数据发布到具有名称为XMLdata的Textarea的URL。 我的XMl数据已准备就绪,位于XDocument Sendingxml = xml中; 但是我试过的邮政编码不起作用。 主要是因为我不知道如何将XML数据放在postData变量中,因为它只接受字符串。 这是我的代码:

XDocument Sendingxml = xml; // string Sendingxml = "  5 CrosskeyCaliforniaCA92620Condo456432013070100158346681a598cf23f412095f6092c281823e611160 "; // Create a request using a URL that can receive a post. WebRequest request = WebRequest.Create("https://test.bluebookcva.net/order/testdirectplugin/3"); // Set the Method property of the request to POST. request.Method = "POST"; // Create POST data and convert it to a byte array. string postData = "XMLData=" + Sendingxml; //My problem is here as I need postData as XDocument. byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded"; // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. Stream dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. WebResponse response = request.GetResponse(); // Display the status. Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. Console.WriteLine(responseFromServer); // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); 

一般来说:

发布XML数据并获取响应(作为字符串)的简单方法示例如下:

 public string postXMLData(string destinationUrl, string requestXml) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl); byte[] bytes; bytes = System.Text.Encoding.ASCII.GetBytes(requestXml); request.ContentType = "text/xml; encoding='utf-8'"; request.ContentLength = bytes.Length; request.Method = "POST"; Stream requestStream = request.GetRequestStream(); requestStream.Write(bytes, 0, bytes.Length); requestStream.Close(); HttpWebResponse response; response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { Stream responseStream = response.GetResponseStream(); string responseStr = new StreamReader(responseStream).ReadToEnd(); return responseStr; } return null; } 

在您的具体情况:

代替:

 request.ContentType = "application/x-www-form-urlencoded"; 

使用:

 request.ContentType = "text/xml; encoding='utf-8'"; 

另外,删除:

 string postData = "XMLData=" + Sendingxml; 

并替换:

 byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

有:

 byte[] byteArray = Encoding.UTF8.GetBytes(Sendingxml.ToString()); 

AlliterativeAlice的例子给了我极大的帮助。 在我的情况下,我正在谈论的服务器不喜欢在内容类型中使用utf-8单引号。 它失败了一般的"Server Error" ,并花了几个小时来弄清楚它不喜欢什么:

 request.ContentType = "text/xml; encoding=utf-8";