How to make a basic XML API Request using C#

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements

How to make a basic XML API Request using C#

L0 Member

Hello, 

 

I'm trying to learn how to use the API with a C# Client.

 

I'm able to make a API request, using a web browser, but can someone, give me an example in C#.

 

So if I wanted to use this example:

 

Make a call to get system information, which returns the IP address, hostname, and model of your firewall.

 

'https://firewall//api/?type=op&cmd=<show><system><info></info></system></show>&key=apikey

response status="success">

<result>

<system>

<hostname>firewall</hostname>

<ip-address>10.27.0.8</ip-address>

 

I'd like to get this back into a form textbox, or a label.

 

This will help me to learn how to use this API and Web Requests.

 

Thanks,

 

-Troy

 

1 REPLY 1

L2 Linker

This should get you started.

It's a console application that retrieve's the firewalls HostName, IP adress and model.

 

using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Xml;

namespace PAN_API
{
    class Program
    {
        static string FirewallIP = "******";
        static string FirewallAPI = "******"; 
        static void Main(string[] args)
        {
            try
            {
                string requestURL = "https://" + FirewallIP + "/api/?type=op&cmd=<show><system><info></info></system></show>&key=" + FirewallAPI;
                WebRequest request = WebRequest.Create(requestURL);
                request.Proxy = null;
                request.Credentials = CredentialCache.DefaultCredentials;
                ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);
                string responseFromServer = reader.ReadToEnd();
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(responseFromServer);
                Console.WriteLine("hostname: " + xmlDoc.GetElementsByTagName("hostname")[0].InnerText);
                Console.WriteLine("ip: " + xmlDoc.GetElementsByTagName("ip-address")[0].InnerText);
                Console.WriteLine("model: " + xmlDoc.GetElementsByTagName("model")[0].InnerText);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.Read();
            }
        }
        public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }
    }
}

 

  • 2541 Views
  • 1 replies
  • 0 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!