The following code sample presents ready-to-use client application written in VB.NET.
Comments
Class Request implements sending a request packet to Plesk server via HTTP and receiving the response packet. It has the following members:
|
Holds the host name (IP address) of Plesk Control Panel to which the request packet is sent. Is "localhost" by default. |
|
Holds Plesk Administrator login. Is "admin" by default. |
|
Holds Plesk Administrator password. Is "setup" by default. |
|
Holds the version of API RPC protocol used for interaction with Plesk. Allowed values: 1.3.5.1 | 1.4.0.0 | 1.4.1.0 | 1.4.1.1 | 1.4.1.2 | 1.4.2.0 | 1.5.0.0 | 1.5.1.0 | 1.5.2.0 |1.5.2.1. Default value: "1.4.2.0". |
|
A handler used to receive schema validation errors. |
|
Holds the URL of Plesk Agent that will handle the request packet on the server side. |
|
Holds the URL of the validation schema that will be applied to the request packet before it is sent to the server side. |
|
Holds the URL of the validation schema that will be applied to the response packet after it is received on the client side. |
|
Gets the request packet (in the form of the XmlDocument object) to its input parameter. Sends a request and gets the response in the form of the XmlDocument object. |
|
Gets the request packet (a stream) to its input parameter. Validates the request packet using the validation schema. Invokes the |
|
Gets the URI of the request packet (XML file) to its input parameter. Validates the request packet using the validation schema. Invokes the |
|
Forms an HTTP request: puts the HTTP header and serialized XML packet to the object of type HttpWebRequest. Returns this object. |
|
Gets the URI of the validation schema and a string reader (with the URI of the XML packet to validate) to its input parameters. Validates the packet and returns it structured as a tree (an XmlDocument object). |
|
Gets the HTTP request packet (HttpWebRequest object) to its input parameter. Sends the packet via HTTP, receives the response packet from the server, validates it using the validation schema, and returns the response XML packet structured as a tree (an XmlDocument object). |
Class Program implements the 'client' console application.
|
An entry point to the PleskApiRpcClient application. Obtains an array of arguments (strings) to its input parameter. The arguments are: [0] - the host name (IP address) of the Plesk control panel, [1] - Plesk Administrator login, [2] - Plesk Administrator password, [3] - the API RPC protocol used, [4] - a path of the XML file with an XML request packet. The function forms a request packet of type Request (see above), validates it, sends a request to Plesk, and prints out the resulting XML packet. |
|
The function verifies the remote SSL certificate to authenticate the server. If the server is not authenticated, returns false. |
|
Validation error event handler. |
|
The function outputs the response packet (an XmlDocument object) to the console. |
The client application can be started from command line as follows:
PleskApiRpcClient 192.0.2.168 admin_login admin_passwd 1.4.2.0 "c:\requests\request.xml"
The request packet is passed in the request.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<packet version="1.4.2.0">
<client>
<add>
<gen_info>
<cname>LogicSoft Ltd.</cname>
<pname>Stephen Lowell</pname>
<login>stevelow</login>
<passwd>steve78</passwd>
<status>0</status>
<phone>416 907 9944</phone>
<fax>928 752 3905</fax>
<email>host@logicsoft.net</email>
<address>105 Brisbane Road, Unit 2</address>
<city>Toronto</city>
<state/>
<pcode/>
<country>CA</country>
</gen_info>
<limits>
<disk_space>100000</disk_space>
<max_dom>50</max_dom>
<max_subdom>250</max_subdom>
<max_webapps>30</max_webapps>
<max_traffic>50000</max_traffic>
<max_db>200</max_db>
<mysql_dbase_space>50000</mysql_dbase_space>
<max_shared_ssl_links>50</max_shared_ssl_links>
<expiration>1134616208</expiration>
</limits>
<permissions>
<create_domains>true</create_domains>
<manage_phosting>true</manage_phosting>
<manage_quota>false</manage_quota>
<manage_subdomains>true</manage_subdomains>
<change_limits>true</change_limits>
<manage_dns>true</manage_dns>
<manage_log>true</manage_log>
<manage_anonftp>true</manage_anonftp>
<manage_webapps>true</manage_webapps>
<manage_sh_access>true</manage_sh_access>
<manage_maillists>true</manage_maillists>
<make_dumps>true</make_dumps>
<remote_access_interface>true</remote_access_interface>
<cp_access>true</cp_access>
<manage_domain_aliases>true </manage_domain_aliases>
</permissions>
</add>
</client>
</packet>
The response packet received from the server can look as follows:
<?xml version="1.0"?>
<packet version="1.4.2.0">
<client>
<add>
<result>
<status>ok</status>
<id>2</id>
</result>
</add>
</client>
</packet>
This packet returns the result of the add operation and the unique identifier of the client account just created and registered in Plesk database.
Code sample
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_login" 'Administrator Login
Public Password As String = "admin_setup" 'Administrator Password
Public Protocol As String = "1.4.2.0" 'API RPC Version Protocol
' 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