使用XML与远程服务器进行交互
最近在做的一个项目其中的一部分是与远程服务器进行交互,确定身份验证的合法性,于是编写了SendRequest方法
此方法发送给远程服务器XML请求,服务器经过处理后,返回XML回应,由此方法接收到后进行返回。
1 protected string SendRequest(string strXML)
2 {
3 string str = ""; //双方协定的XML格式
4 Encoding encoding = Encoding.UTF8; //接收页面
5 string strUrl = "http://localhost:14360/WebSite16/Handler.ashx";
6 byte[] data = encoding.GetBytes(strXML); //准备请求...
7 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
8 myRequest.Method = "POST";
9 myRequest.ContentType = "text/xml;charset=utf-8";
10 myRequest.ContentLength = data.Length; //身份认证,特别注意,参数用户名,密码
11 NetworkCredential cred = new NetworkCredential("wcadmin", "wcadmin");
12 myRequest.Credentials = cred; //在信息请求头部加入验证信息,不然验证不通过
13 myRequest.PreAuthenticate = true;
14 Stream newStream = myRequest.GetRequestStream(); //发送数据
15 newStream.Write(data, 0, data.Length);
16 newStream.Close();
17 WebResponse response = myRequest.GetResponse();
18 Stream resStream = response.GetResponseStream();
19 StreamReader sr = new StreamReader(resStream, System.Text.Encoding.UTF8);
20 str = sr.ReadToEnd();//接收返回值,返回值可以是xml
21 resStream.Close();
22 sr.Close();
23 return str;
2 {
3 string str = ""; //双方协定的XML格式
4 Encoding encoding = Encoding.UTF8; //接收页面
5 string strUrl = "http://localhost:14360/WebSite16/Handler.ashx";
6 byte[] data = encoding.GetBytes(strXML); //准备请求...
7 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
8 myRequest.Method = "POST";
9 myRequest.ContentType = "text/xml;charset=utf-8";
10 myRequest.ContentLength = data.Length; //身份认证,特别注意,参数用户名,密码
11 NetworkCredential cred = new NetworkCredential("wcadmin", "wcadmin");
12 myRequest.Credentials = cred; //在信息请求头部加入验证信息,不然验证不通过
13 myRequest.PreAuthenticate = true;
14 Stream newStream = myRequest.GetRequestStream(); //发送数据
15 newStream.Write(data, 0, data.Length);
16 newStream.Close();
17 WebResponse response = myRequest.GetResponse();
18 Stream resStream = response.GetResponseStream();
19 StreamReader sr = new StreamReader(resStream, System.Text.Encoding.UTF8);
20 str = sr.ReadToEnd();//接收返回值,返回值可以是xml
21 resStream.Close();
22 sr.Close();
23 return str;
24 }
解析返回的XML的方法如下代码片段:
1 public bool CheckUser(string token)
2 {
3 bool flag = false; //验证成功与否的标识
4 //向服务器发出验证XML信息
5 string requestXML = "";
6 requestXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ecity><msgname>XXXXXX</msgname><msgversion>1.0.0</msgversion><transactionid>000000</transactionid><timestamp>"
7 + DateTime.Now.ToString("yyyyMMddhhmmss") + "</timestamp><msgsender>abc</msgsender><svccont><token>"+token+"</token></svccont></ecity>";
8
9 //发送验证的XML并获取返回的XML信息
10 string responseXML = "";
11 responseXML= SendRequest(requestXML);
12
13 //解析XML信息
14 XmlDocument xmlDoc = new XmlDocument();
15 xmlDoc.LoadXml(responseXML);
16 XmlNodeList nodes = xmlDoc.SelectNodes("/ecity/msgname");
17 if (nodes.Count > 0)
18 {
19 //首先判断是否接口正确
20 if (nodes[0].InnerText.Trim().ToLower() == "getuserinforesp")
21 {
22 nodes = xmlDoc.SelectNodes("/ecity/result/rspcode");
23 if (nodes.Count > 0)
24 {
25 //表示验证通过
26 if (nodes[0].InnerText.Trim() == "0")
27 {
28 flag = true;
29 }
30 }
31 }
32 }
33
34 return flag;
2 {
3 bool flag = false; //验证成功与否的标识
4 //向服务器发出验证XML信息
5 string requestXML = "";
6 requestXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ecity><msgname>XXXXXX</msgname><msgversion>1.0.0</msgversion><transactionid>000000</transactionid><timestamp>"
7 + DateTime.Now.ToString("yyyyMMddhhmmss") + "</timestamp><msgsender>abc</msgsender><svccont><token>"+token+"</token></svccont></ecity>";
8
9 //发送验证的XML并获取返回的XML信息
10 string responseXML = "";
11 responseXML= SendRequest(requestXML);
12
13 //解析XML信息
14 XmlDocument xmlDoc = new XmlDocument();
15 xmlDoc.LoadXml(responseXML);
16 XmlNodeList nodes = xmlDoc.SelectNodes("/ecity/msgname");
17 if (nodes.Count > 0)
18 {
19 //首先判断是否接口正确
20 if (nodes[0].InnerText.Trim().ToLower() == "getuserinforesp")
21 {
22 nodes = xmlDoc.SelectNodes("/ecity/result/rspcode");
23 if (nodes.Count > 0)
24 {
25 //表示验证通过
26 if (nodes[0].InnerText.Trim() == "0")
27 {
28 flag = true;
29 }
30 }
31 }
32 }
33
34 return flag;
35 }
以上代码为真实项目中的代码,经过修改后附上的