Interaction with remote servers using XML and xml
In a recent project, part of the project was to interact with the remote server to determine the legitimacy of identity authentication. Therefore, the SendRequest method was compiled.
This method is used to send an XML request to the remote server. After processing, the server returns an XML response, which is returned after the method is received.
1 protected string SendRequest (string strXML)
2 {
3 string str = ""; // XML format agreed by both parties
4 Encoding encoding = Encoding. UTF8; // receiving page
5 string strUrl = "http: // localhost: 14360/WebSite16/Handler. ashx ";
6 byte [] data = encoding. GetBytes (strXML); // prepare the request...
7 HttpWebRequest myRequest = (HttpWebRequest) WebRequest. Create (strUrl );
8 myRequest. Method = "POST ";
9 myRequest. ContentType = "text/xml; charset = UTF-8 ";
10 myRequest. ContentLength = data. Length; // identity authentication. Pay special attention to the parameter username and password.
11 NetworkCredential cred = new NetworkCredential ("wcadmin", "wcadmin ");
12 myRequest. Credentials = cred; // Add verification information to the information request header. Otherwise, the verification fails.
13 myRequest. PreAuthenticate = true;
14 Stream newStream = myRequest. GetRequestStream (); // send data
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 (); // receives the returned value. The returned value can be xml.
21 resStream. Close ();
22 sr. Close ();
23 return str;
24}
The following code snippet is used to parse the returned XML:
1 public bool CheckUser (string token)
2 {
3 bool flag = false; // identify whether the verification is successful or not
4 // send XML verification information to the server
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 // send the verified XML and obtain the returned XML Information
10 string responseXML = "";
11 responseXML = SendRequest (requestXML );
12
13 // Parse XML Information
14 XmlDocument xmlDoc = new XmlDocument ();
15 xmlDoc. LoadXml (responseXML );
16 XmlNodeList nodes = xmlDoc. SelectNodes ("/ecity/msgname ");
17 if (nodes. Count> 0)
18 {
19 // first determine whether the interface is correct
20 if (nodes [0]. InnerText. Trim (). ToLower () = "getuserinforesp ")
21 {
22 nodes = xmlDoc. SelectNodes ("/ecity/result/rspcode ");
23 if (nodes. Count> 0)
24 {
25 // indicates that the verification is successful
26 if (nodes [0]. InnerText. Trim () = "0 ")
27 {
28 flag = true;
29}
30}
31}
32}
33
34 return flag;
35}
The above code is the code in the real project. After modification,