zoukankan      html  css  js  c++  java
  • 使用XML与远程服务器进行交互

    最近在做的一个项目其中的一部分是与远程服务器进行交互,确定身份验证的合法性,于是编写了SendRequest方法

    此方法发送给远程服务器XML请求,服务器经过处理后,返回XML回应,由此方法接收到后进行返回。

     1 protected string SendRequest(string strXML) { 
     2     string str = ""; //双方协定的XML格式 
     3     Encoding encoding = Encoding.UTF8; //接收页面       
     4     string strUrl = "http://localhost:14360/WebSite16/Handler.ashx"; 
     5     byte[] data = encoding.GetBytes(strXML); //准备请求... 
     6     HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl); 
     7     myRequest.Method = "POST"; 
     8     myRequest.ContentType = "text/xml;charset=utf-8"; 
     9     myRequest.ContentLength = data.Length; //身份认证,特别注意,参数用户名,密码 
    10     NetworkCredential cred = new NetworkCredential("wcadmin", "wcadmin"); 
    11     myRequest.Credentials = cred; //在信息请求头部加入验证信息,不然验证不通过 
    12     myRequest.PreAuthenticate = true; 
    13     Stream newStream = myRequest.GetRequestStream(); //发送数据 
    14     newStream.Write(data, 0, data.Length); 
    15     newStream.Close(); 
    16     WebResponse response = myRequest.GetResponse(); 
    17     Stream resStream = response.GetResponseStream(); 
    18     StreamReader sr = new StreamReader(resStream, System.Text.Encoding.UTF8); 
    19     str = sr.ReadToEnd();//接收返回值,返回值可以是xml 
    20     resStream.Close(); 
    21     sr.Close(); 
    22     return str; 
    23 }

     解析返回的XML的方法如下代码片段:

     1 public bool CheckUser(string token) {
     2     bool flag = false;  //验证成功与否的标识
     3     //向服务器发出验证XML信息
     4     string requestXML = "";
     5     requestXML = "<?xml version="1.0" encoding="UTF-8"?><ecity><msgname>XXXXXX</msgname><msgversion>1.0.0</msgversion><transactionid>000000</transactionid><timestamp>"
     6         + DateTime.Now.ToString("yyyyMMddhhmmss") + "</timestamp><msgsender>abc</msgsender><svccont><token>"+token+"</token></svccont></ecity>";
     7 
     8     //发送验证的XML并获取返回的XML信息
     9     string responseXML = "";
    10     responseXML= SendRequest(requestXML);
    11 
    12     //解析XML信息
    13     XmlDocument xmlDoc = new XmlDocument();
    14     xmlDoc.LoadXml(responseXML);
    15     XmlNodeList nodes = xmlDoc.SelectNodes("/ecity/msgname");
    16     if (nodes.Count > 0)
    17     {
    18         //首先判断是否接口正确
    19         if (nodes[0].InnerText.Trim().ToLower() == "getuserinforesp")
    20         {
    21             nodes = xmlDoc.SelectNodes("/ecity/result/rspcode");
    22             if (nodes.Count > 0)
    23             {
    24                 //表示验证通过
    25                 if (nodes[0].InnerText.Trim() == "0")
    26                 {
    27                     flag = true;
    28                 }
    29             }
    30         }
    31     }
    32 
    33     return flag;
    34 }

    转自:http://www.cnblogs.com/shunyao8210/archive/2011/09/19/2181075.html

  • 相关阅读:
    CodeForces 510C Fox And Names (拓扑排序)
    Codeforces 1153D Serval and Rooted Tree (简单树形DP)
    HDU 6437 Problem L.Videos (最大费用)【费用流】
    Luogu P3381 (模板题) 最小费用最大流
    Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)
    Codeforces 1144F Graph Without Long Directed Paths (DFS染色+构造)
    HDU 2204 Eddy's 爱好 (容斥原理)
    Codeforces 939E Maximize! (三分 || 尺取)
    Codeforces 938D. Buy a Ticket (最短路+建图)
    CodeForces 959E Mahmoud and Ehab and the xor-MST (MST+找规律)
  • 原文地址:https://www.cnblogs.com/olmlo/p/3576272.html
Copyright © 2011-2022 走看看