接口示例http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline
/// <summary>
/// HttpPost请求
/// </summary>
/// <param name="Url">链接</param>
/// <param name="DataParam">参数</param>
/// <returns></returns>
public string HttpPost(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.Timeout = 300000;
byte[] bytes = Encoding.UTF8.GetBytes(postDataStr);
request.ContentLength = bytes.Length;
Stream writer = request.GetRequestStream();
writer.Write(bytes, 0, bytes.Length);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
response.Close();
return retString;
}
调用接口返回到 retString 的 XML内容的字符串
retString ="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<string xmlns=“http://WebXml.com.cn/”>Y"
string responseStrFime = new RequestHelper().NowHttpPost(Url, DataParam);
XmlDocument doc = new XmlDocument();//新建一个XML文档
doc.LoadXml(responseStrFime);//将字符串转换成XML文档
XmlNodeList weather_nodes = doc.GetElementsByTagName("string");//读取XML文档的父节点
string State = weather_nodes[0].InnerText;
此时节点State为Y
结束
更多节点访问方式
int id = int.Parse(node.ChildNodes[0].InnerText);//InnerText节点包含的所有文本内容
string lang = node.ChildNodes[1].Attributes[0].InnerText;
string name = node.ChildNodes[1].InnerText;
int damage = int.Parse(node.ChildNodes[2].InnerText);
参考以下部分代码
原文链接:https://blog.csdn.net/weixin_33950757/article/details/90410970