2009-04-03 12 views
12

Sto cercando un esempio di come, in C#, inserire un documento xml nel corpo del messaggio di una richiesta http e quindi analizzare la risposta. Ho letto la documentazione, ma vorrei solo vedere un esempio se ce n'è uno disponibile. Qualcuno ha un esempio?C# Xml in Http messaggio di richiesta messaggio Corpo

grazie

risposta

30
private static string WebRequestPostData(string url, string postData) 
{ 
    System.Net.WebRequest req = System.Net.WebRequest.Create(url); 

    req.ContentType = "text/xml"; 
    req.Method = "POST"; 

    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postData); 
    req.ContentLength = bytes.Length; 

    using (Stream os = req.GetRequestStream()) 
    { 
     os.Write(bytes, 0, bytes.Length); 
    } 

    using (System.Net.WebResponse resp = req.GetResponse()) 
    { 
     if (resp == null) return null; 

     using (System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream())) 
     { 
      return sr.ReadToEnd().Trim(); 
     } 
    } 
} 
Problemi correlati