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

问题描述 投票:1回答:1

我对REST调用比较陌生,可能是C#中的初学者。我正在尝试向Qualys发出REST调用以获取设备信息。我的问题是,当我执行foreach循环迭代List时,控制台应用程序失败,只需在输入我的凭据后退出。我已经验证API正在接受我的凭据。

请记住,我使用HttpBasicAuthenticator连接到Qualys,我正在使用RestSharp和XmlSerializer的组合。我不知道我是否正确这样做,但这是我到目前为止所得到的。我想完全使用RestSharp,但我对于为反序列化部分做什么感到困惑,所以我尝试使用来自Internet的一些示例使用XmlSerializer来处理它。

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)。

c# xml-serialization restsharp
1个回答
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; }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.