1. Please take a little time for this simple survey! Thank you for participating!
    Dismiss Notice
  2. Dear Pleskians, please read this carefully! New attachments and other rules Thank you!
    Dismiss Notice
  3. Dear Pleskians, I really hope that you will share your opinion in this Special topic for chatter about Plesk in the Clouds. Thank you!
    Dismiss Notice

API-RPC code sample in VB.NET

Discussion in 'Plesk for Windows - 8.x and Older' started by Rainbow@, Nov 20, 2007.

  1. Rainbow@

    Rainbow@ Guest

    0
     
  2. Rainbow@

    Rainbow@ Guest

    0
     
    PleskApiRpcClient.txt -> PleskApiRpcClient.vb
     
  3. Rainbow@

    Rainbow@ Guest

    0
     
    Imports System
    Imports System.Net
    Imports System.Text
    Imports System.IO
    Imports System.Xml
    Imports System.Xml.Schema
    Imports System.Security.Cryptography.X509Certificates
    Imports System.Net.Security

    Namespace PleskApiRpcClient
    Public Class Request
    ' Public interface
    Public Hostname As String = "localhost" 'Control Panel Hostname
    Public Login As String = "admin" 'Administrator Login
    Public Password As String = "setup" 'Administrator Password
    Public Protocol As String = "1.4.2.0" 'API RPC Version Protocol
    ' Now available:
    ' 1.3.5.1
    ' 1.4.0.0
    ' 1.4.1.0
    ' 1.4.1.1
    ' 1.4.1.2
    ' 1.4.2.0
    ' Handler for receiving information about document type definition (DTD),
    ' XML-Data Reduced (XDR) schema, and XML Schema definition language (XSD)
    ' schema validation errors.
    Public XmlSchemaValidation As ValidationEventHandler

    Public ReadOnly Property AgentEntryPoint As String
    Get
    Return ("https://" & Me.Hostname & ":8443/enterprise/control/agent.php")
    End Get
    End Property

    Public ReadOnly Property InputValidationSchema As String
    Get
    Return String.Concat(New String() { "https://", Me.Hostname, ":8443/schemas/rpc/", Me.Protocol, "/agent_input.xsd" })
    End Get
    End Property

    Public ReadOnly Property OutputValidationSchema As String
    Get
    Return String.Concat(New String() { "https://", Me.Hostname, ":8443/schemas/rpc/", Me.Protocol, "/agent_output.xsd" })
    End Get
    End Property

    Public Function Send(ByVal packet As XmlDocument) As XmlDocument
    Dim request As HttpWebRequest = Me.SendRequest(packet.OuterXml)
    Return Me.GetResponse(request)
    End Function

    Public Function Send(ByVal packet As Stream) As XmlDocument
    Using reader As TextReader = New StreamReader(packet)
    Return Me.Send(Me.ParseAndValidate(reader, Me.InputValidationSchema))
    End Using
    End Function

    Public Function Send(ByVal packetUri As String) As XmlDocument
    Using reader As TextReader = New StreamReader(packetUri)
    Return Me.Send(Me.ParseAndValidate(reader, Me.InputValidationSchema))
    End Using
    End Function
    ' Private interface
    '

    ' Sending a request message
    '
    Private Function SendRequest(ByVal message As String) As HttpWebRequest
    Dim request As HttpWebRequest = DirectCast(WebRequest.Create(Me.AgentEntryPoint), HttpWebRequest)
    request.Method = "POST"
    request.Headers.Add("HTTP_AUTH_LOGIN", Me.Login)
    request.Headers.Add("HTTP_AUTH_PASSWD", Me.Password)
    request.ContentType = "text/xml"
    request.ContentLength = message.Length
    Dim bytes As Byte() = New ASCIIEncoding().GetBytes(message)
    Using stream As Stream = request.GetRequestStream
    stream.Write(bytes, 0, message.Length)
    End Using
    Return request
    End Function
    ' Parsing and validating packet
    '
    Private Function ParseAndValidate(ByVal xml As TextReader, ByVal schemaUri As String) As XmlDocument
    Dim schemas As New XmlSchemaSet
    schemas.Add(Nothing, schemaUri)
    Dim settings As New XmlReaderSettings
    If (Not Me.XmlSchemaValidation Is Nothing) Then
    AddHandler settings.ValidationEventHandler, New ValidationEventHandler(AddressOf Me.XmlSchemaValidation.Invoke)
    End If
    settings.ValidationType = ValidationType.Schema
    settings.ValidationFlags = (settings.ValidationFlags Or XmlSchemaValidationFlags.ProcessSchemaLocation)
    settings.Schemas = schemas
    Dim document As New XmlDocument
    Using reader As XmlReader = XmlReader.Create(xml, settings)
    document.Load(reader)
    End Using
    Return document
    End Function

    Private Function GetResponse(ByVal request As HttpWebRequest) As XmlDocument
    Using response As HttpWebResponse = DirectCast(request.GetResponse, HttpWebResponse)
    Using stream As Stream = response.GetResponseStream
    Using reader As TextReader = New StreamReader(stream)
    return Me.ParseAndValidate(reader, Me.OutputValidationSchema)
    End Using
    End Using
    End Using
    End Function




    End Class

    Friend Class Program

    Shared Sub Main(ByVal args As String())
    If (args.Length < 5) Then
    Console.WriteLine("Usage: PleskApiRpcClient <Hostname> <Login> <Password> <Protocol> <Request>")
    Console.WriteLine(" ")
    Console.WriteLine(" Hostname - Control Panel hostname")
    Console.WriteLine(" Login - Administrator login")
    Console.WriteLine(" Password - Administrator password")
    Console.WriteLine(" Protocol - API RPC protocol version")
    Console.WriteLine(" Request - Request file path (*.xml)")
    Else
    ' Verifies the remote Secure Sockets Layer (SSL) certificate
    ' used for authentication.
    ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf Program.RemoteCertificateValidation)
    Dim request As New Request
    request.XmlSchemaValidation = New ValidationEventHandler(AddressOf Program.XmlSchemaValidation)
    request.Hostname = args(0) ' "10.49.8.120";
    request.Login = args(1) ' "admin";
    request.Password = args(2) ' "setup";
    request.Protocol = args(3) ' "1.4.2.0";
    Dim packetUri As String = args(4) ' "request.xml";
    Try
    Program.PrintResult(request.Send(packetUri))
    Catch exception As Exception
    Console.WriteLine("Request error: {0}", exception.Message)
    End Try
    End If
    End Sub

    ' The following method is invoked by the RemoteCertificateValidationDelegate.
    Private Shared Function RemoteCertificateValidation(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
    If (sslPolicyErrors <> SslPolicyErrors.RemoteCertificateNotAvailable) Then
    Return True
    End If
    Console.WriteLine("Certificate error: {0}", sslPolicyErrors)
    ' Do not allow this client to communicate with unauthenticated servers.
    Return False
    End Function
    '
    Private Shared Sub XmlSchemaValidation(ByVal sender As Object, ByVal e As ValidationEventArgs)
    Console.WriteLine("Validation error: {0}", e.Message)
    End Sub

    Private Shared Sub PrintResult(ByVal document As XmlDocument)
    Dim w As New XmlTextWriter(Console.Out)
    w.Formatting = Formatting.Indented
    document.WriteTo(w)
    w.Flush
    Console.WriteLine
    End Sub

    End Class

    End Namespace
     
Loading...