Interacting with a remote server using XML

Source: Internet
Author: User

One of the most recent projects that was done was to interact with the remote server to determine the legitimacy of the authentication, and then wrote the SendRequest method

This method sends the XML request to the remote server, which, after processing, returns an XML response, which is returned after the method is received.

1 protected string SendRequest (String strxml)
2 {
3 String str = ""; XML format of the mutual agreement
4 Encoding Encoding = Encoding.UTF8; Receive page
5 string strURL = "Http://localhost:14360/WebSite16/Handler.ashx";
6 byte[] data = encoding. GetBytes (strxml); Prepare request ...
7 HttpWebRequest myrequest = (HttpWebRequest) webrequest.create (strURL);
8 Myrequest.method = "POST";
9 Myrequest.contenttype = "Text/xml;charset=utf-8";
Ten myrequest.contentlength = data. Length; Identity authentication, special attention, parameter username, password
One networkcredential cred = new NetworkCredential ("Wcadmin", "wcadmin");
Myrequest.credentials = cred; Add validation information to the header of the information request, or the validation does not pass
Myrequest.preauthenticate = true;
Stream newstream = Myrequest.getrequeststream (); Send data
Newstream.write (data, 0, data. Length);
Newstream.close ();
WebResponse response = Myrequest.getresponse ();
Stream Resstream = Response. GetResponseStream ();
StreamReader sr = new StreamReader (Resstream, System.Text.Encoding.UTF8);
str = Sr. ReadToEnd ();//Receive return value, the return value can be XML
Resstream.close ();
Sr. Close ();
At the return of STR;

24}

The method for parsing the returned XML is the following code snippet:

1 public bool CheckUser (string token)
2 {
3 BOOL flag = FALSE; Identification of success or absence of validation
4//issue validation XML 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><svc Cont><token> "+token+" </token></svccont></ecity> ";
8
9//Send the validated XML and get the returned XML information
Ten string responsexml = "";
Responsexml= SendRequest (RequestXML);
12
13//Parsing XML information
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.loadxml (Responsexml);
XmlNodeList nodes = xmldoc.selectnodes ("/ecity/msgname");
if (nodes. Count > 0)
18 {
19//First determine if the interface is correct
if (Nodes[0]. Innertext.trim (). ToLower () = = "Getuserinforesp")
21 {
nodes = Xmldoc.selectnodes ("/ecity/result/rspcode");
if (nodes. Count > 0)
24 {
25//indicates verification through
if (Nodes[0]. Innertext.trim () = = "0")
27 {
Flag = true;
29}
30}
31}
32}
33
return flag;

35}

The code above is the code in the real project, and the modified

Interacting with a remote server using XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.