How to use XML API with Asp.net

rawat

Member
May 21, 2010
7
0
51
Hello,

How to use XML API with Asp.net ,please help for coding,how to use XML API for Create Account .

Actually i m using

XML and JSON APIs

But facing Authentication Error.and I don't know PHP or Pearl.....

ThanX
rawat
 

cPanelDavidN

Well-Known Member
Staff member
Dec 17, 2009
571
3
68
Houston, TX
cPanel Access Level
Root Administrator
Hi Rawat,

I'm not too familiar with ASP, but I imagine it shouldn't be hard. Your ASP script must:
1) make a URL request
2) that contains proper authentication headers

You can test how the XML-API works directly in your browser. Manually logging into your server (this will set the authentication data in subsequent headers) and then make a direct XML-API call. A good test URL would be:
Code:
http://yourserver.com:2086/xml-api/version
Maybe someone with ASP experience can provide an example piece of code.

Regards,
-DavidN


UPDATE: placed example url in code block so the wysiwyg doesn't try to make a direct URL
 
Last edited:

MattDees

Well-Known Member
Apr 29, 2005
416
1
243
Houston, TX
cPanel Access Level
Root Administrator
if I remember correctly the work flow for .NET auth header via the default method is:

  1. Client sets up request object
  2. client sends request to server w/o auth header
  3. server responds stating that auth is needed
  4. client sends request again w/ auth header
  5. server accepts authentication

The problem is that the way that cpsrvd responds to auth requests doesn't work with how .NET handles HTTP Basic auth in it's default implementation (this can be said for apache under certain configurations as well).

The change that has to happen, is that:

  1. Client set up request object
  2. client sends request w/ auth headers
  3. server authenticates

check out:

ie-soft.de Blog - C#: Create a WebRequest with HTTP Basic Authentication

for an example of how to do this, if this does not work for you, I've seen a few examples on how to do this and will hunt one down for you.
 

rawat

Member
May 21, 2010
7
0
51
XML API Authentication

Hello Sir,

I m trying this code for Authentication but facing Error

string url = "http://test.com:2086";
WebRequest myReq = WebRequest.Create(url);

string username = "Test";
string password = "test";
string usernamePassword = username + ":" + password;
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));

WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
 

MattDees

Well-Known Member
Apr 29, 2005
416
1
243
Houston, TX
cPanel Access Level
Root Administrator
So, I just verified that this works for authentication:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ListAccount
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://129.169.1.2:2086/xml-api/listaccts";
            HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;

            string user = "root";
            string pwd = "x";

            string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(user + ":" + pwd));
            req.PreAuthenticate = true;
            req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
            req.Headers.Add("Authorization", auth);

            req.UserAgent = "cPanel .NET C# authenticator test script";
            WebResponse resp = req.GetResponse();
            Stream receiveStream = resp.GetResponseStream();
            StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
            string content = reader.ReadToEnd();

            System.Console.WriteLine(content);
            System.Console.Read();   
        }
    }
}
 

rawat

Member
May 21, 2010
7
0
51
XML API For Create Account with ASP.net

Hello Sir,

but we will call XML API for Create Account with XML API.Please Provide a example....

ThanX


-------------------------







So, I just verified that this works for authentication:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ListAccount
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://129.169.1.2:2086/xml-api/listaccts";
            HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;

            string user = "root";
            string pwd = "x";

            string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(user + ":" + pwd));
            req.PreAuthenticate = true;
            req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
            req.Headers.Add("Authorization", auth);

            req.UserAgent = "cPanel .NET C# authenticator test script";
            WebResponse resp = req.GetResponse();
            Stream receiveStream = resp.GetResponseStream();
            StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
            string content = reader.ReadToEnd();

            System.Console.WriteLine(content);
            System.Console.Read();   
        }
    }
}
 

B12Org

Well-Known Member
Jul 15, 2003
691
1
168
Seattle Washington
cPanel Access Level
Root Administrator
When I try that it works well for everything except for create account - that one always throws an exception - although the command does work as I get the account created email and if I try again it immediately fails becuase the user exists.

{"Unable to read data from the transport connection: The connection was closed."}
[System.IO.IOException]: {"Unable to read data from the transport connection: The connection was closed."}
Data: {System.Collections.ListDictionaryInternal}
HelpLink: null
InnerException: null
Message: "Unable to read data from the transport connection: The connection was closed."
Source: "System"
StackTrace: " at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)\r\n at System.IO.StreamReader.ReadBuffer()\r\n at System.IO.StreamReader.ReadToEnd()\r\n
Per the message the error occurs here - on the 2nd line

StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
String strResponse = reader.ReadToEnd();




it seems to occur on any of those commands that send out emails - create account, suspend account, etc - but none of the other commands do that.

We dont use proxies or anything like that.
Any ideas why?
 
Last edited:

Eric

Well-Known Member
Nov 25, 2007
754
14
143
Texas
cPanel Access Level
Root Administrator
Twitter
Howdy,

I'm able to use the same program Matt posted and changed the string url line to be this
Code:
string url = "http://grimlock:2086/xml-api/createacct?username=myuser&password=test$1234&domain=domain.tld";
I get a created account. I'm using a simple console app. Give that a try. If you have problems let me know I'll help as best I can.

Thanks!
 

B12Org

Well-Known Member
Jul 15, 2003
691
1
168
Seattle Washington
cPanel Access Level
Root Administrator
Hi,

We are using this:

which basically is sent using this code
PHP:
    private String SendRequest(String method, String variables, String server)
    {
        //Dont bail from a self signed cert or other cert error
        if (Global.UseSSL)
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidation);

        String url = String.Format("http{0}://{1}:{2}/xml-api/{3}?{4}", Global.UseSSL ? "s" : "", server, Global.UseSSL ? "2087" : "2086", method, variables);
        HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;

        String auth = String.Format("Basic {0}", Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(String.Format("{0}:{1}", Global.WHMUser, Global.WHMPass))));
        req.PreAuthenticate = true;
        req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
        req.Headers.Add("Authorization", auth);

        WebResponse resp = req.GetResponse();
        Stream receiveStream = resp.GetResponseStream();
        StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
[B]        String strResponse = reader.ReadToEnd();[/B]

        return strResponse;
    }
The bold line throws an exception every single time only when using the createaccount or un/suspendaccount functions. None of the other 20 method names that we have tried cause the error (like generate csr, install IPs, etc).

seems wierd that its always these two that cause the error - and always the same error on the same line.

Can you provide the code you are using, or perhaps a tweak to what we are doing above?
 

B12Org

Well-Known Member
Jul 15, 2003
691
1
168
Seattle Washington
cPanel Access Level
Root Administrator
access log yes
63.229.62.199 - root [09/10/2010:17:19:15 -0000] "GET /xml-api/createacct?username=myuser3&password=__HIDDEN__&[email protected]&domain=myuser3.somedomain.com&cpmod=x3&quota=0&bwlimit=0&ip=n&cgi=1&frontpage=0&hasshell=0&maxftp=unlimited&maxsql=unlimited&maxpop=0&maxlst=0&maxsub=unlimited&featurelist=default&maxpark=unlimited&maxaddon=unlimited&useregns=0&reseller=0&mxcheck=remote HTTP/1.1" 200 0 "" ""
error log no