获取需要登录认证的远程数据
///获取远程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; }