2013-07-08 14 views
24

Ho bisogno di inviare HTTP dati XML a un URL che ha Textarea con il nome di XMLdata. I miei dati XMl sono pronti e si trovano all'interno di XDocument Sendingxml = xml; ma il codice postale che ho provato non funziona. Principalmente perché non so come inserire i dati XML nella variabile PostData in quanto accetta solo stringhe. Questo è il mio codice:Dati XML post HTTP in C#

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

risposta

67

In generale:

Un esempio di un modo semplice per inviare i dati XML e ottenere la risposta (come una stringa) sarebbe la seguente funzione:

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

Nella tua situazione specifica:

Invece di:

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

uso:

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

Inoltre, rimuovere:

string postData = "XMLData=" + Sendingxml; 

e sostituire:

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

con:

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

Grazie, ma che dire di questo 2 linee, archi postData = "XMLData =" + Sendingxml; byte [] byteArray = Encoding.UTF8.GetBytes (postData); Ho bisogno di avere postData come XDocument, perché ho bisogno di inviare XMLData come formato xml non stringa. – Alma

+0

Mi dispiace per quello. Ho aggiornato la mia risposta con una soluzione più completa. – AlliterativeAlice

+0

Grazie, ho sostituito con tutto ciò che hai detto qui, ma ancora non reindirizza nemmeno all'URL. – Alma

2

L'esempio di AlliterativeAlice mi ha aiutato moltissimo. Nel mio caso, però, il server con cui stavo parlando non amava avere virgolette singole attorno allo utf-8 nel tipo di contenuto. Non è riuscito con un generico "Server Error" e ci sono voluti ore per capire che cosa non gli piaceva:

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

Grazie! Avevo lo stesso problema. Rimosso le zecche e tutto bene. – Keith