天天看点

Request.InputStream 将数据作为XML数据发送

将数据作为XML数据发送,例如:

Request.InputStream 将数据作为XML数据发送

public void PostXml(string url, string xml)

Request.InputStream 将数据作为XML数据发送

Request.InputStream 将数据作为XML数据发送

   byte[] bytes = Encoding.UTF8.GetBytes(xml); 

Request.InputStream 将数据作为XML数据发送

   HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url); 

Request.InputStream 将数据作为XML数据发送

   request.Method = "POST"; 

Request.InputStream 将数据作为XML数据发送

   request.ContentLength = bytes.Length; 

Request.InputStream 将数据作为XML数据发送

   request.ContentType = "text/xml"; 

Request.InputStream 将数据作为XML数据发送

   using (Stream requestStream = request.GetRequestStream()) { 

Request.InputStream 将数据作为XML数据发送

     requestStream.Write(bytes, 0, bytes.Length); 

Request.InputStream 将数据作为XML数据发送

   } 

Request.InputStream 将数据作为XML数据发送
Request.InputStream 将数据作为XML数据发送

   HttpWebResponse response = (HttpWebResponse) request.GetResponse(); 

Request.InputStream 将数据作为XML数据发送

   if (response.StatusCode != HttpStatusCode.OK) { 

Request.InputStream 将数据作为XML数据发送

     string message = String.Format("POST failed. Received HTTP {0}", 

Request.InputStream 将数据作为XML数据发送

     response.StatusCode); 

Request.InputStream 将数据作为XML数据发送

     throw new ApplicationException(message); 

Request.InputStream 将数据作为XML数据发送
Request.InputStream 将数据作为XML数据发送

}

接收端通过Request.InputStream读取:

Request.InputStream 将数据作为XML数据发送

byte[] byts = new byte[Request.InputStream.Length];

Request.InputStream 将数据作为XML数据发送

Request.InputStream.Read(byts,0,byts.Length);

Request.InputStream 将数据作为XML数据发送

string req = System.Text.Encoding.Default.GetString(byts);

Request.InputStream 将数据作为XML数据发送

req = Server.UrlDecode(req);

对于完整的XML数据,可以: 

Request.InputStream 将数据作为XML数据发送

xmlDoc = new XmlDocument();

Request.InputStream 将数据作为XML数据发送

xmlDoc.load(Request.InputStream);

本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/archive/2013/03/18/2966096.html,如需转载请自行联系原作者