c# 发送post请求到服务器,并解析得到的xml格式的字符串

  1. 以下是 HTTP POST 请求和响应示例。所显示的占位符需替换为实际值。
  2. POST /web/FileService.asmx/DoAction HTTP/1.1
  3. Host: www.***.com
  4. Content-Type: application/x-www-form-urlencoded
  5. Content-Length: length
  6. proxyId=string&action=string

     有网站,如www.***.com上有一个webservice接口,比如DoAction,客户端发送请求之后,返回跟某个ID相关的一些信息,比如文件列表等,客户端发送代码,解析代码如下:

 

网站支持httppost方法,如上的代码,返回结果实例如下,

  1. HTTP/1.1 200 OK
  2. Content-Type: text/xml; charset=utf-8
  3. Content-Length: length
  4. "1.0" encoding="utf-8"?>
  5. <string xmlns="http://www.***.com/">stringstring>

 在客户端(web)上发送代码:

  1.             string service = "http://www.***.com/web/FileService.asmx/DoAction";
  2.             string contenttype = "application/x-www-form-urlencoded";//更网站该方法支持的类型要一致
  3. //根据接口,写参数
  4.             string para = "proxyId=123456";
  5.             para += "&action=*************";
  6.             //发送请求
  7.             HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(service);
  8.             myRequest.Method = "POST";
  9.             myRequest.ContentType = contenttype;
  10.             myRequest.ContentLength = para.Length;
  11.             Stream newStream = myRequest.GetRequestStream();
  12.             // Send the data.
  13.             ASCIIEncoding encoding = new ASCIIEncoding();
  14.             byte[] postdata = encoding.GetBytes(para);
  15.             newStream.Write(postdata, 0, para.Length);
  16.             newStream.Close();
  17.             // Get response
  18.             HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  19.             StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  20.             string content = reader.ReadToEnd();//得到结果

上面的content就是返回的字符串结果,可能是这样子的:

  1.     <string xmln="www.***.com">
  2.     
  3.     0
  4.     
  5.     
  6.     "1/" URL=/"******/"/>
  7.     "2/" URL=/"******/"/>
  8.     
  9.     
  10.     
  11.     string>

然后利用XmlDocument等解析结果:

  1. //解析接口返回值,这里选用XML格式的解析          
  2. XmlDocument xmlDoc = new XmlDocument();
  3. string xmlrespons = "";
  4. xmlDoc.LoadXml(content);
  5. XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
  6. nsmgr.AddNamespace("""http://ws.95013.com/");//这个要跟string后面的xmln的字符串一致
  7. XmlElement root = xmlDoc.DocumentElement;
  8. xmlrespons = root.InnerText;
  9. byte[] byteSource = Encoding.GetEncoding("utf-8").GetBytes(xmlrespons);   
  10. MemoryStream ms = new MemoryStream(byteSource);   
  11. XmlTextReader xmlreader=new XmlTextReader(ms);   
  12. string sname,svalue;
  13. while(xmlreader.Read())   
  14. {   
  15.     if(xmlreader.NodeType == XmlNodeType.Element)
  16.     {
  17.     if ("Item".Equals(xmlreader.Name))
  18.     {
  19.         while (xmlreader.MoveToNextAttribute())
  20.         {
  21.         switch (xmlreader.Name)
  22.         {
  23.         case "ID":
  24.         //做操作 
  25.                 string str1=xmlreader.Value;
  26.         break;
  27.         case "URL":
  28.         string str2= xmlreader.Value; 
  29.         break;
  30.         ...                             default:
  31.         break;
  32.         }
  33.          }
  34.          xmlreader.MoveToElement();//读完一个几点后要让指针后移
  35.     }
  36.      }
  37.      if ("Result".Equals(xmlreader.Name))
  38.      {//对resault结点的解析
  39.     svalue = xmlreader.ReadString();
  40.         ...
  41.       }
  42. }
  43. //解析完了将释放变量    
  44. xmlreader.Close();   
  45. ms.Close(); 

你可能感兴趣的:(c# 发送post请求到服务器,并解析得到的xml格式的字符串)