从POST读取XML响应

时间:2016-01-20 00:22:42

标签: c# asp.net xml webforms

我正在尝试做类似的事情,但我不知道如何实际阅读XML响应。 @NikolaiDante推荐的语法实际上会去哪里?有没有人有一个完整的工作示例?我会在那里发表评论,但代表不够高......

以下是原帖的链接: Read XML Response From Page

我总是使用汉塞尔曼的例子,因为我理解它,并且很容易遵循

public static string HttpPost(string URI, string Parameters) 
{
   System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
   req.Proxy = new System.Net.WebProxy(ProxyString, true);
   req.ContentType = "application/x-www-form-urlencoded";
   req.Method = "POST";
   byte [] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
   req.ContentLength = bytes.Length;
   System.IO.Stream os = req.GetRequestStream ();
   os.Write (bytes, 0, bytes.Length); //Push it out there
   os.Close ();
   System.Net.WebResponse resp = req.GetResponse();
   if (resp== null) return null;
   System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
   return sr.ReadToEnd().Trim();
}

以下是该网站的链接,以便您可以看到并获得信用: enter link description here

1 个答案:

答案 0 :(得分:1)

只要拿回你正在返回的字符串就行了......

string result = HttpPost(url, parameters);
XmlDocument xml = new XmlDocument();
xml.LoadXml(result);

或在您的HttpPost方法中..

var stream = resp.GetResponseStream();
XmlDocument xml = new XmlDocument();
xml.Load(stream);

您可以引用this link来解析实际的XML文档