The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

WHM XML-API using C# - Help

Discussion in 'cPanel Developers' started by serverportugal, Jan 17, 2009.

  1. serverportugal

    serverportugal Registered

    Joined:
    Jan 17, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    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)
     
    #1 serverportugal, Jan 17, 2009
    Last edited: Jan 17, 2009
  2. serverportugal

    serverportugal Registered

    Joined:
    Jan 17, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    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.
     
    #2 serverportugal, Jan 18, 2009
    Last edited: Jan 18, 2009
Loading...

Share This Page