net enviar and c# xml http-headers

enviar - soap request and response in c# net



Datos XML de publicaciĆ³n HTTP en C# (2)

El ejemplo de AlliterativeAlice me ayudó enormemente. En mi caso, sin embargo, el servidor con el que estaba hablando no le gustó tener comillas simples alrededor de utf-8 en el tipo de contenido. Falló con un "Server Error" genérico y le llevó horas descubrir qué no le gustaba:

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

Necesito HTTP publicar datos XML en una URL que tenga Textarea con el nombre de XMLdata. Mis datos XMl están listos y están dentro de XDocument Sendingxml = xml; pero el código postal que he probado no funciona. Principalmente porque no sé cómo poner los datos XML en la variable de PostData, ya que solo acepta la cadena. Este es mi código:

XDocument Sendingxml = xml; // string Sendingxml = "<?xml version=1.0 encoding=UTF-8> <PluginData> <Credential UserName=avosfieldagent01 AuthenticationToken=61cc3957744742dca238c4dd7cbca702 /><Session><PropertyAddress>5 Crosskey</PropertyAddress><PropertyAddress2/><PropertyCity>California</PropertyCity><PropertyState>CA</PropertyState><PropertyZip>92620</PropertyZip><PropertyType>Condo</PropertyType><SourceReferenceId>45643</SourceReferenceId><SessionId>2013070100158346</SessionId><SessionCompleteReturnURL/><CustomerId/><BluebookOrderCheckSum>681a598cf23f412095f6092c281823e6</BluebookOrderCheckSum><BluebookOrderId>11160</BluebookOrderId> </Session></PluginData>"; // 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();


En general:

Un ejemplo de una manera fácil de publicar datos XML y obtener la respuesta (como una cadena) sería la siguiente función:

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; }

En su situación específica:

En lugar de:

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

utilizar:

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

Además, elimina:

string postData = "XMLData=" + Sendingxml;

Y reemplazar:

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

con:

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