【发布时间】:2009-04-03 23:00:05
【问题描述】:
我正在寻找一个示例,说明如何在 C# 中将 xml 文档放入 http 请求的消息正文中,然后解析响应。我已经阅读了文档,但如果有可用的示例,我只想看一个示例。谁有例子?
谢谢
【问题讨论】:
标签: c# xml httpwebrequest httpwebresponse
我正在寻找一个示例,说明如何在 C# 中将 xml 文档放入 http 请求的消息正文中,然后解析响应。我已经阅读了文档,但如果有可用的示例,我只想看一个示例。谁有例子?
谢谢
【问题讨论】:
标签: c# xml httpwebrequest httpwebresponse
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();
}
}
}
【讨论】:
【讨论】: