码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET中将数据作为XML数据发送 使用 Request.InputStream 接收

时间:2014-09-10 12:18:20      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:http   os   io   使用   for   数据   sp   on   c   

将数据作为XML数据发送,例如:
public void PostXml(string url, string xml)
{
byte[] bytes = Encoding.UTF8.GetBytes(xml);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "text/xml";
using (Stream requestStream = request.GetRequestStream()) {
requestStream.Write(bytes, 0, bytes.Length);
}HttpWebResponse response = (HttpWebResponse) request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK) {
string message = String.Format("POST failed. Received HTTP {0}",
response.StatusCode);
throw new ApplicationException(message);
}
}
接收端通过Request.InputStream读取:
byte[] byts = new byte[Request.InputStream.Length];
Request.InputStream.Read(byts,0,byts.Length);
string req = System.Text.Encoding.Default.GetString(byts);
req = Server.UrlDecode(req);
对于完整的XML数据,可以:
xmlDoc = new XmlDocument();
xmlDoc.load(Request.InputStream);

ASP.NET中将数据作为XML数据发送 使用 Request.InputStream 接收

标签:http   os   io   使用   for   数据   sp   on   c   

原文地址:http://www.cnblogs.com/mili3/p/3964087.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!