///獲取遠程xml信息
string strUrl = "www.xxx.com/get/2323"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl); request.Method = "post"; request.ContentType = "text/xml;charset=utf-8"; request.ContentLength = 0; NetworkCredential cred = new NetworkCredential("用戶名", "密碼"); request.Credentials = cred; //在信息請求頭部加入驗證信息,否則驗證不經過 request.PreAuthenticate = true; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); Response.Write(retString);
//處理返回的xml
public bool CheckUser(string token) { bool flag = false; //驗證成功與否的標識 //向服務器發出驗證XML信息 string requestXML = ""; requestXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ecity><msgname>XXXXXX</msgname><msgversion>1.0.0</msgversion><transactionid>000000</transactionid><timestamp>" + DateTime.Now.ToString("yyyyMMddhhmmss") + "</timestamp><msgsender>abc</msgsender><svccont><token>" + token + "</token></svccont></ecity>"; //發送驗證的XML並獲取返回的XML信息 string responseXML = ""; responseXML = SendRequest(requestXML); //解析XML信息 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(responseXML); XmlNodeList nodes = xmlDoc.SelectNodes("/ecity/msgname"); if (nodes.Count > 0) { //首先判斷是否接口正確 if (nodes[0].InnerText.Trim().ToLower() == "getuserinforesp") { nodes = xmlDoc.SelectNodes("/ecity/result/rspcode"); if (nodes.Count > 0) { //表示驗證經過 if (nodes[0].InnerText.Trim() == "0") { flag = true; } } } } return flag; }