惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
News | PayPal Newsroom
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
Forbes - Security
Forbes - Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
Hacker News: Ask HN
Hacker News: Ask HN
H
Heimdal Security Blog
D
Docker
Cloudbric
Cloudbric
P
Privacy International News Feed
S
Security Affairs
TaoSecurity Blog
TaoSecurity Blog
博客园 - 聂微东
WordPress大学
WordPress大学
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Tenable Blog
Scott Helme
Scott Helme
人人都是产品经理
人人都是产品经理
Recent Announcements
Recent Announcements
P
Palo Alto Networks Blog
小众软件
小众软件
L
LINUX DO - 最新话题
美团技术团队
Google Online Security Blog
Google Online Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
The Hacker News
The Hacker News
Webroot Blog
Webroot Blog
T
Tor Project blog
G
Google Developers Blog
A
About on SuperTechFans
Y
Y Combinator Blog
K
Kaspersky official blog
A
Arctic Wolf
量子位
I
InfoQ
V
Visual Studio Blog
T
Troy Hunt's Blog
C
Cybersecurity and Infrastructure Security Agency CISA
J
Java Code Geeks
博客园 - 【当耐特】
GbyAI
GbyAI

博客园 - IT爱好者

C# 自定义打印 C# 打印,不显示打印进度对话框 如何解决迅雷插件导致IE10崩溃的问题 ASP.NET MVC 使用Jquery Uploadify 在非IE浏览器下Http Error的解决方案 c#操作access,update语句不执行的解决办法 解决:System.Data.SqlClient.SqlError: FILESTREAM 功能被禁用 服务器“**”上的MSDTC不可用的解决办法 通过Package Manager Console 向VS2010安装 EFCodeFirst 删除SVN遗留的无用文件 解决"System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本" 修改IIS默认的localhost名称 IIS服务器应用程序不可用的解决办法 小米手机指令大全 Java与.NET DES加密解密互转 获取执行SQL语句的返回结果 ASP.NET中如何向页面写入JavaScript脚本内容 Oracle自增列创建方法 ASP.net中用JSON序列化对象 android Listview 拖动时背景为黑色问题
使用XML与远程服务器进行交互
IT爱好者 · 2011-09-19 · via 博客园 - IT爱好者

最近在做的一个项目其中的一部分是与远程服务器进行交互,确定身份验证的合法性,于是编写了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; 

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;

35     }

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