serverportugal

Registered
Jan 17, 2009
2
0
51
Hello,
Anyone here, used XML-API with C#, obviously from a remote windows server ?

If so, how can use the Remote Access Key ?

I need some light upon this, if anyone coded anything that uses whm xml-api on C#, help will be appreciated.

Thank you
JL

ps. sorry for my poor english.

(PORTUGAL)
 
Last edited:

serverportugal

Registered
Jan 17, 2009
2
0
51
Using WHM's XML-API on Microsoft C#

Well, I am posting a reply to my own question, because I could achieve the result I need about using WHM's XML-API on Microsoft C#.
To anyone with the same question I had yesterday, here's a little piece of code to help you out:

First, add the following lines to your app.config file, or you will get an error when executing the webrequest:
Code:
  <system.net>
    <settings>
      <httpWebRequest useUnsafeHeaderParsing = "true"/>
    </settings>
  </system.net>
This example, suspends a CPanel account (johndoe):

Code:
//The url command to do what you need
string url = "http://your-whm-server:2086/xml-api/suspendacct?user=johndoe&reason=no%20payment";

WebRequest wreq = WebRequest.Create(url);

//Your credentials (the same you use to login on WHM panel)
CredentialCache MyCredential = new CredentialCache();
MyCredential.Add(new Uri("http://your-whm-server.com:2086/"), "Basic", new NetworkCredential("your-whm-username", "your-whm-password"));
wreq.Credentials = MyCredential;

//I set the timeout to 8 seconds...
wreq.Timeout = 8000;

WebResponse wres = wreq.GetResponse();
if (((HttpWebResponse)wres).StatusDescription == "OK")
{
	wres.Close(); //to free resources
	//Code if OK
}
else
{
	wres.Close(); //to free resources
	//Code if not OK
}

NOTE: Remember that to get the XML-API result, you need to write some code to read the xml stream, because the lines above based on 'HttpWebResponse', will only return the connection result to the server.

That's it, hope it helps.
 
Last edited: