2009-04-03 24 views
12

Tôi đang tìm một ví dụ về cách, trong C#, để đặt một tài liệu xml trong nội dung thư của yêu cầu http và sau đó phân tích cú pháp phản hồi. Tôi đã đọc tài liệu nhưng tôi chỉ muốn xem một ví dụ nếu có sẵn. Có ai có một ví dụ?C# Xml trong Http Đăng Yêu cầu Nội dung Thông báo

cảm ơn

Trả lời

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(); 
     } 
    } 
} 
Các vấn đề liên quan