Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

cPanel login rejected with API through C#

Discussion in 'cPanel Developers' started by James_B, Jan 12, 2011.

  1. James_B

    James_B Registered

    Jan 12, 2011
    Likes Received:
    Trophy Points:
    Hi all,

    I'm trying to use C# (in a desktop application) to connect to my cPanel account (eventually to create a database but for now just to connect). I'm currently testing this on a shared hosting account with Hostgator, it's not a reseller account.

    Below is the code I'm using to connect (adapted from info I found on this forum), I've ensured that the port is for cPanel (not a WHM port) and I've run the request successfully in my web browser after logging into cPanel.

    It throws the error "The remote server returned an error: (403) Forbidden" or if I use the commented out string for the request (simply the url + cpanel port) it gives the error "The remote server returned an error: (401) Unauthorized".

    I would really appreciate any help/advice :)

            private string GetURLResponse(string url)
                WebRequest wreq = WebRequest.Create(url);
                //Your credentials (the same you use to login on WHM panel)
                CredentialCache MyCredential = new CredentialCache();
                MyCredential.Add(new Uri(""), "Basic", new NetworkCredential("user", "pass"));
                wreq.Credentials = MyCredential;
                wreq.Timeout = 8000;
                WebResponse wres = wreq.GetResponse();
                if (((HttpWebResponse)wres).StatusDescription == "OK")
                    wres.Close(); //to free resources
                    //Code if OK
                    return "OK";
                    wres.Close(); //to free resources
                    //Code if not OK
                    return "Not OK";
    public bool CreateConnection()
                string strURL = "";
                //string strURL = "";
                return true;
    #1 James_B, Jan 12, 2011
    Last edited: Jan 12, 2011
  2. cPanelDavidN

    cPanelDavidN Integration Developer

    Dec 17, 2009
    Likes Received:
    Trophy Points:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi James,

    Unfortunately, I'm not familiar with C# so I can't comment too much or provide any useful feedback to you. Your query URL looks fine. The only thing that I can recommend is that instead of "user" as the URL parameter, use "cpanel_xmlapi_user". This will ensure proper parameter parsing if you use an API call that itself has a parameter of "user". For example, if you call the Ftp::addftp (which adds a new FTP account), one of the necessary parameters is "user", the name of the new FTP account. That said, it should have any affect on the call you're making now. Additionally, if you're making API calls to port 2082 or 2083, the "cpanel_xmlapi_user"/"user" parameter should not be necessary; only when making calls on ports 2086 or 2087. If you pass it (and it's the same as the username you authenticated with), there shouldn't be a problem...only if, for some reason, you pass something different.

    Besides that, I can only recommend that you use the HTTPS ports, 2083 and 2087. Some cPanel servers have the non-SSL ports turned off. But again, I think in your situation, the difference is mitigated since, as you've mentioned, you can make the exact same URL call in the browser without fail.

    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice