星空网 > 软件开发 > ASP.net

使用XML与远程服务器进行交互

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

此方法发送给远程服务器

 

使用XML与远程服务器进行交互
 1 protected string SendRequest(string str 2     { 
 3         string str = ""; //双方协定的 4         Encoding encoding = Encoding.UTF8; //接收页面       
 5         string strUrl = "http://localhost:14360/WebSite16/Handler.ashx"; 
 6         byte[] data = encoding.GetBytes(str 7         HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl); 
 8         myRequest.Method = "POST"; 
 9         myRequest.ContentType = "text/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();//接收返回值,返回值可以是21         resStream.Close(); 
22         sr.Close(); 
23         return str; 
使用XML与远程服务器进行交互

24     }

 

 解析返回的使用XML与远程服务器进行交互
 1 public bool CheckUser(string token)
 2     {
 3         bool flag = false;  //验证成功与否的标识
 4         //向服务器发出验证 5         string request 6         request 7             + DateTime.Now.ToString("yyyyMMddhhmmss") + "</timestamp><msgsender>abc</msgsender><svccont><token>"+token+"</token></svccont></ecity>";
 8 
 9         //发送验证的10         string response11         response12 
13         //解析14         15         16         17         if (nodes.Count > 0)
18         {
19             //首先判断是否接口正确
20             if (nodes[0].InnerText.Trim().ToLower() == "getuserinforesp")
21             {
22                 nodes = 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;
使用XML与远程服务器进行交互

35     }

 

以上代码为真实项目中的代码,经过修改后附上的 




原标题:使用XML与远程服务器进行交互

关键词:xml

xml
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

谷歌引流教程:https://www.goluckyvip.com/tag/31916.html
谷歌营销 Tao:https://www.goluckyvip.com/tag/31917.html
谷歌优化引流:https://www.goluckyvip.com/tag/31918.html
谷歌站外广告投放:https://www.goluckyvip.com/tag/31919.html
已经来临:https://www.goluckyvip.com/tag/3192.html
谷歌站外引流:https://www.goluckyvip.com/tag/31920.html
【再放信号】美国Etsy即将放开中国卖家入驻,官方邮件你收到了吗?:https://www.kjdsnews.com/a/1836640.html
安庆市周边免费景点 安庆免费游玩的地方有哪些?:https://www.vstour.cn/a/365186.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流