如何使用RestSharp遍历REST调用中的列表?

时间:2019-04-11 12:10:32

标签: c# xml-serialization restsharp

我对REST调用比较陌生,可能是C#中级的初学者。我试图对Qualys进行REST调用以获取设备信息。我的问题是,当我执行一个foreach循环以遍历列表时,控制台应用程序失败,原因是在输入凭据后退出。我已验证该API接受我的凭据。

请记住,我正在使用HttpBasicAuthenticator连接到Qualys,并且我在使用RestSharp和XmlSerializer的组合。我不知道我是否正确地做到了这一点,但这就是到目前为止。我想完全使用RestSharp,但是对于反序列化部分该怎么做,我完全感到困惑,因此我尝试使用XmlSerializer并通过Internet上的一些示例对其进行处理。

class Program
    {
        const string BaseUrl = "https://qualysapi.qualys.com/";        

        static void Main(string[] args)
        {        
            Console.WriteLine("Username: ");
            var username = Console.ReadLine();
            Console.WriteLine("Password: ");
            var password = Console.ReadLine();

            var _client = new RestClient(BaseUrl);
            _client.Authenticator = new HttpBasicAuthenticator(username, password);

            var request = new RestRequest("api/2.0/fo/appliance/", Method.GET);
            request.AddHeader("X-Requested-With", "REST");
            request.AddParameter("action", "list");
            request.AddParameter("output_mode", "brief");
            var response = _client.Execute(request).Content;

            XmlRootAttribute xRoot = new XmlRootAttribute();
            xRoot.ElementName = "APPLIANCE_LIST_OUTPUT";
            xRoot.IsNullable = true;
            XmlSerializer serializer = new XmlSerializer(typeof(List<Appliance>), xRoot);
            StringReader stringReader = new StringReader(response);
            List<Appliance> applianceValues = (List<Appliance>)serializer.Deserialize(stringReader);

            foreach (var i in applianceValues)
            {
                Console.WriteLine("ID: " + i.ID);
                Console.WriteLine("UUID: " + i.UUID);
                Console.WriteLine("Name: " + i.NAME);
                Console.WriteLine("Software Version: " + i.SOFTWARE_VERSION);
                Console.WriteLine("Status: " + i.STATUS);
            }          
        }        
    }

 public class Appliance
    {
        [XmlElement(ElementName = "id")]
        public string ID { get; set; }

        [XmlElement(ElementName = "uuid")]
        public int UUID { get; set; }

        [XmlElement(ElementName = "name")]
        public string NAME { get; set; }

        [XmlElement(ElementName = "software_ver")]
        public int SOFTWARE_VERSION { get; set; }

        [XmlElement(ElementName = "status")]
        public string STATUS { get; set; }
    }

响应:我通过注释掉foreach循环并开始执行操作来得到此响应 Console.WriteLine(response);

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE APPLIANCE_LIST_OUTPUT SYSTEM "https://qualysapi.qualys.com/api/2.0/fo/appliance/appliance_list_output.dtd">
<APPLIANCE_LIST_OUTPUT>
  <RESPONSE>
    <DATETIME>2019-04-11T12:31:49Z</DATETIME>
    <APPLIANCE_LIST>
      <APPLIANCE>
        <ID>111111</ID>
        <UUID>1a2b3c4d-14qw-f00f-6744-g7455556bnf4</UUID>
        <NAME>My Appliance</NAME>
        <SOFTWARE_VERSION>2.6</SOFTWARE_VERSION>
        <RUNNING_SLICES_COUNT>0</RUNNING_SLICES_COUNT>
        <RUNNING_SCAN_COUNT>0</RUNNING_SCAN_COUNT>
        <STATUS>Online</STATUS>
      </APPLIANCE>
      <APPLIANCE>
        <ID>222222</ID>
        <UUID>bv51gh82-g496-88g8-8999-11abcd4567kk</UUID>
        <NAME>My Other Appliance</NAME>
        <SOFTWARE_VERSION>2.6</SOFTWARE_VERSION>
        <RUNNING_SLICES_COUNT>0</RUNNING_SLICES_COUNT>
        <RUNNING_SCAN_COUNT>0</RUNNING_SCAN_COUNT>
        <STATUS>Offline</STATUS>
      </APPLIANCE>
    </APPLIANCE_LIST>
  </RESPONSE>
</APPLIANCE_LIST_OUTPUT>

现在,我的目标只是向控制台输出XML中某些字段的漂亮清单。任何帮助将不胜感激。

注意:出于机密性,我已更改了一些值(ID,UUID,NAME)。

1 个答案:

答案 0 :(得分:0)

尝试以下。看来您需要禁用DTD处理:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication108
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            StringReader sReader = new StringReader(xml);

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.DtdProcessing = DtdProcessing.Ignore;
            XmlReader reader = XmlReader.Create(sReader, settings);

            XmlSerializer serializer = new XmlSerializer(typeof(ApplianceOutput));
            ApplianceOutput applicance = (ApplianceOutput)serializer.Deserialize(reader);

        }
    }
    [XmlRoot("APPLIANCE_LIST_OUTPUT")]
    public class ApplianceOutput
    {
        [XmlElement("RESPONSE")]
        public Response response { get; set; }
    }
    [XmlRoot("RESPONSE")]
    public class Response
    {
        public DateTime date { get; set; }
        [XmlArray("APPLIANCE_LIST")]
        [XmlArrayItem("APPLIANCE")]
        public List<Appliance> appliances { get; set; }
    }
    public class Appliance
    {
        public string ID { get; set; }
        public string UUID { get; set; }
        public string NAME { get; set; }
        public string SOFTWARE_VERSION { get; set; }
        public int RUNNING_SLICES_COUNT { get; set; }
        public int RUNNING_SCAN_COUNT { get; set; }
        public string STATUS { get; set; }
    }

}