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.

cPanel login rejected with API through C#

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

  1. James_B

    James_B Registered

    Joined:
    Jan 12, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    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 :)

    Code:
    
            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("http://domain.com:2082"), "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";
                }
                else
                {
                    wres.Close(); //to free resources
                    //Code if not OK
                    return "Not OK";
                }
            }
    
    
    public bool CreateConnection()
            {
                string strURL = "http://domain.com:2082/xml-api/cpanel?user=user&cpanel_xmlapi_module=Email&cpanel_xmlapi_func=listpopswithdisk&cpanel_xmlapi_version=2&domain=hostgator-review.info";
                //string strURL = "http://domain.com:2082/";
                MessageBox.Show(GetURLResponse(strURL));
                return true;
            }
    
    Thanks,
    James
     
    #1 James_B, Jan 12, 2011
    Last edited: Jan 12, 2011
  2. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    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.

    Regards,
    -DavidN
     
Loading...

Share This Page