Archive

Posts Tagged ‘Onlinenic’

Onlinenic API in C# (Part 2)

March 18, 2011 11 comments

In this article we will discuss about some functionalities of Onlinenic API. We should know that for each function we can establish a new connection. (A good practice is placing connect and login method in the constructor of a class).

Onlinenic API with C#

This article will cover domain availability checking, retrieving domain information and creation of domain. I tried to make you familiar to code the rest yourself, should you encountered any question you can email or comment me.

Domain availability checking:

This function is one of the simplest function in this API. You just need to send your domain name and the extension code of your desired domain. For example “google.com” as domain name and “0” for extension which is “.com”.

/// <summary>
/// Checks the availability of domain name

/// </summary>

/// <param name="domainName">Name of domain e.g. yahoo.com</param>

/// <param name="domainExt">Extension number of domain e.g. 800 for biz</param>

/// <returns>true means domain is available and false means domain is not available</returns>

public bool CheckDomain(string domainName, string domainExt)

{

string chksum = "";

string HashedPass = getMd5Hash(Password);

Guid guid = Guid.NewGuid();

chksum = MemberID + HashedPass + guid.ToString() + "checkdomain" + domainExt + domainName;

chksum = getMd5Hash(chksum);

StringBuilder sb = new StringBuilder();

sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");

sb.Append("<request>");

sb.Append("<category>domain</category>");

sb.Append("<action>CheckDomain</action>");

sb.Append("<params>");

//Domain extension should be the code of a domain name. For example 0 for .com

sb.Append("     <param name=\"domaintype\">" + domainExt.ToString() + "</param>");

//Domain name should be the complete form of a domain name. For example microsoft.com

sb.Append("     <param name=\"domain\">" + domainName + "</param>");

sb.Append("</params>");

sb.Append("<cltrid>" + guid.ToString() + "</cltrid>");

sb.Append("<chksum>" + chksum + "</chksum>");

sb.Append("</request>");
String responseData = String.Empty;

// Buffer to store the response bytes.

Byte[] data;

data = System.Text.Encoding.ASCII.GetBytes(sb.ToString());

// Send the message to the connected TcpServer.

stream.Write(data, 0, data.Length);

//Reads data and returns it to the application

Int32 bytes = stream.Read(data, 0, data.Length);

responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

if (!responseData.Contains("Command completed successfully"))

{

return false;

}

return responseData.Contains("&lt;data name=\"avail\"&gt;1&lt;/data&gt;");

}

Retrieving domain information:

This section is really important because finding the real meaning of this command wasted a lot of my time just to debug and search about this job. Onlinenic API does not retrieve information of all domains; it only does it for domains which are registered under your account. So don’t waste your time to get this functionality.

/// <summary>;

/// Returns the information about a registered domain under your account

/// </summary>

/// <param name="domainName">Name of domain e.g. yahoo.com</param>

/// <param name="domainExt">Extension number of domain e.g. 800 for biz</param>

/// <returns>An xml-based string response</returns>

public string getDomainInfo(string domainName, string domainExt)

{

string chksum = "";

string HashedPass = getMd5Hash(Password);

Guid guid = Guid.NewGuid();

chksum = MemberID + HashedPass + guid.ToString() + "infodomain" +

domainExt + domainName;

chksum = getMd5Hash(chksum);

StringBuilder sb = new StringBuilder();

sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");

sb.Append("<request>");

sb.Append("<category>domain</category>");

sb.Append("<action>InfoDomain</action>");

sb.Append("<params>");

sb.Append("     <param name=\"domaintype\">" + domainExt + "</param>");

sb.Append("     <param name=\"domain\">" + domainName + "</param>");

sb.Append("</params>");

sb.Append("<cltrid>" + guid.ToString() + "</cltrid>");

sb.Append("<chksum>" + chksum + "</chksum>");

sb.Append("</request>");
String responseData = String.Empty;

Byte[] data;

data = System.Text.Encoding.ASCII.GetBytes(sb.ToString());

// Send the message to the connected TcpServer.

stream.Write(data, 0, data.Length);

//Reads data and returns it to the application

Int32 bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

byte[] byteArray = Encoding.ASCII.GetBytes(responseData);

MemoryStream memStream = new MemoryStream(byteArray);

if (!responseData.Contains("Command completed successfully"))

{

return "";

}

else

{

responseData = ParseXMLstream(memStream);

}

return responseData;

}

Domain Creation:

This topic also was one of the most challenging sections of my job because comments in the published documents are totally unclear.

Creation of domain contact:

In order to create/register a domain you should create/register an entity called contact. This entity contains information about persons in charged with this domain, their contact information and some other. It is important to know that creation of contact for each domain extension has a common part and a specific part which differs from one domain extension to another. You should know that for .EU and .Asia domain extensions you should specify country codes related to these regions [you can refer to the document for detailed information].

I tried to build it clearly and use comments to help you learn it easier.

/// <summary>

/// Creates contact which is required for domain registration

/// </summary>

/// <param name="domainExt">Extension number of domain e.g. 800 for biz</param>

/// <param name="name">Name of contact person</param>

/// <param name="organization">Organization of contact person</param>

/// <param name="country">Country of contact person. It is a string with length of 2 e.g. IR for Iran. You should consider domain extension for correct country, for example IR cannot be used as the country of a .EU domain extension</param>

/// <param name="province">Province of contact person</param>

/// <param name="city">City of contact person</param>

/// <param name="street">Address of contact person</param>

/// <param name="postCode">Post code of contact person</param>

/// <param name="phoneNo">Pnone no of contact person</param>

/// <param name="faxNo">Fax number of contact person</param>

/// <param name="email">Email address of contact person</param>

/// <param name="password">Password of contact object</param>

/// <returns>A string of created contact</returns>

public string CreateContact(string domainExt, string name, string organization, string country, string province, string city, string street, string postCode,

string phoneNo, string faxNo, string email, string password)

{

string chksum = "";

string HashedPass = getMd5Hash(Password);

Guid guid = Guid.NewGuid();

chksum = MemberID + HashedPass + guid.ToString() + "crtcontact" + name + organization + email;

chksum = getMd5Hash(chksum);

StringBuilder sb = new StringBuilder();

sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");

sb.Append("<request>");

sb.Append("<category>domain</category>");

sb.Append("<action>CreateContact</action>");

sb.Append("<params>");

sb.Append("<param name=\"domaintype\">" + domainExt + "</param>");

sb.Append("<param name=\"name\">" + name + "</param>");

sb.Append("<param name=\"org\">" + organization + "</param>");

sb.Append("<param name=\"country\">" + country + "</param>");

sb.Append("<param name=\"province\">" + province + "</param>");

sb.Append("<param name=\"city\">" + city + "</param>");

sb.Append("<param name=\"street\">" + stream + "</param>");

sb.Append("<param name=\"postalcode\">" + postCode + "</param>");

sb.Append("<param name=\"voice\">" + phoneNo + "</param>");

sb.Append("<param name=\"fax\">" + faxNo + "</param>");

sb.Append("<param name=\"email\">" + email + "</param>");

sb.Append("<param name=\"password\">" + password + "</param>");

switch (domainExt)

{

case "902": // .EU

sb.Append("<param name=\"euflag\">" + "1" + "</param>");

sb.Append("<param name=\"regtype\">" + "In" + "</param>");

break;

case "220": // .CN

sb.Append("<param name=\"manager\">" + "Your Name" + "</param>");

sb.Append("<param name=\"industry\">" + "S2" + "</param>");

break;

case "905": // .ASIA

sb.Append("<param name=\"ccLocality\">" + "IR" + "</param>");

sb.Append("<param name=\"localitySp\">" + "Tehran" + "</param>");

sb.Append("<param name=\"localityCity\">" + "Tehran" + "</param>");

sb.Append("<param name=\"legalEntityType\">" + "corporation" + "</param>");

sb.Append("<param name=\"identForm\">" + "other" + "</param>");

sb.Append("<param name=\"identNumber\">" + "122211122" + "</param>");

sb.Append("<param name=\"otherIdentForm\">" + "other" + "</param>");

break;

case "806": // .US

sb.Append("<param name=\"AppPurpose\">" + "P1" + "</param>");

sb.Append("<param name=\"NexusCategory\">" + "C12" + "</param>");

break;

}

sb.Append("</params>");

sb.Append("<cltrid>" + guid.ToString() + "</cltrid>");

sb.Append("<chksum>" + chksum + "</chksum>");

sb.Append("</request>");

String responseData = String.Empty;

// Buffer to store the response bytes.

Byte[] data;

data = System.Text.Encoding.ASCII.GetBytes(sb.ToString());

// Send the message to the connected TcpServer.

stream.Write(data, 0, data.Length);

//Reads data and returns it to the application

Int32 bytes = stream.Read(data, 0, data.Length);

responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

byte[] byteArray = Encoding.ASCII.GetBytes(responseData);

MemoryStream memStream = new MemoryStream(byteArray);

if (responseData.Contains("Command completed successfully"))

{

responseData = ParseXMLstream(memStream, "data");

}

return responseData;

}

Registration of a domain:

After creation of one or more contact objects it is feasible to register a domain. You should create at least one contact object for each domain registration. Also you should specify name servers of each domain. You should mention that for registration of different domain extensions, different checksum order should be specified.

/// <summary>

/// Registered an availabe domain under your account. You should build at least one contact before registration of a domain

/// </summary>

/// <param name="domainName">Name of domain e.g. yahoo.com</param>

/// <param name="domainExt">Extension number of domain e.g. 800 for biz</param>

/// <param name="period">The amount of domain registration. e.g. 2 for 2 years</param>

/// <param name="NS1">Name server number 1</param>

/// <param name="NS2">Name server number 2</param>

/// <param name="password">Password of domain object</param>

/// <param name="contact">The created contact of </param>

/// <returns>Returns true if domain is successfully registerd</returns>

public bool createDomain(string domainName, string domainExt, string period,

string NS1, string NS2, string password, string contact)

{

string chksum = "";

string HashedPass = getMd5Hash(Password);

Guid guid = Guid.NewGuid();

switch (domainExt)

{

case "902": //.EU

chksum = MemberID + HashedPass + guid.ToString() + "createdomain" + domainExt + domainName + period + NS1 + NS2 + contact + password;

break;

case "905"://.ASIA

chksum = MemberID + HashedPass + guid.ToString() + "createdomain" + domainExt + domainName +

period + NS1 + NS2 + contact + contact + contact + contact + contact + password;

break;

default:

chksum = MemberID + HashedPass + guid.ToString() + "createdomain" + domainExt + domainName + period + NS1 + NS2 + contact + contact + contact + contact + password;

break;

}

chksum = getMd5Hash(chksum);

StringBuilder sb = new StringBuilder();

sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");

sb.Append("<request>");

sb.Append("<category>domain</category>");

sb.Append("<action>CreateDomain</action>");

sb.Append("<params>");

sb.Append("     <param name=\"domaintype\">" + domainExt + "</param>");

sb.Append("     <param name=\"domain\">" + domainName + "</param>");

sb.Append("     <param name=\"mltype\">" + "0" + "</param>");

sb.Append("     <param name=\"period\">" + period + "</param>");

sb.Append("     <param name=\"dns\">" + NS1 + "</param>");

sb.Append("     <param name=\"dns\">" + NS2 + "</param>");

sb.Append("     <param name=\"registrant\">" + contact + "</param>");

sb.Append("     <param name=\"tech\">" + contact + "</param>");

sb.Append("     <param name=\"billing\">" + contact + "</param>");

sb.Append("     <param name=\"admin\">" + contact + "</param>");

sb.Append("     <param name=\"password\">" + password + "</param>");

sb.Append("</params>");

sb.Append("<cltrid>" + guid.ToString() + "</cltrid>");

sb.Append("<chksum>" + chksum + "</chksum>");

sb.Append("</request>");
String responseData = String.Empty;

// Buffer to store the response bytes.

Byte[] data;

data = System.Text.Encoding.ASCII.GetBytes(sb.ToString());

//Send the message to the connected TcpServer.

stream.Write(data, 0, data.Length);

//Reads data and returns it to the application

Int32 bytes = stream.Read(data, 0, data.Length);

responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bool flag = responseData.Contains("Command completed successfully");

if (!flag)

{

return false;

}

return flag;

}

I wish you found it useful.

Onlinenic API in C# (Part 1)

March 15, 2011 11 comments

Onlinenic API with C#

Some weeks ago I had to build a complete library to work with Onlinenic’s API. After spending about a week on this awful API I managed to develop one.

A technical independent pdf document was released by Onlinenic but it is not clear; however I attached this file to this post. The first is from Onlinenic, the other is from an unknown source.

Because unfortunately I couldn’t find any technical document on the web in any platforms, I decided to write a post about this issue in c#. I hope you find it useful. This article contains: establishing connection, login to API and log out from API. You can read the rest in another postOnlinenic API in C# (Part 2).

Connecting to API:

Firstly, you should know that it only accepts TCP connection. So you should build a TCP connection to connect to this API.

You should send your command in an XML format, AIP also sends its response in this format too.

In order to connect to the API you can use this method:

bool Connect()

{

client = new TcpClient();

// Server name is <a href="http://www.onlinenic.com/">www.onlinenic.com</a> and port no is 30009

client.Connect(ServerName, PortNo);

stream = client.GetStream();

// String to store the response ASCII representation.

String responseData = String.Empty;

// Buffer to store the response bytes.

Byte[] data = new Byte[256];

// Read the first batch of the TcpServer response bytes.

Int32 bytes = stream.Read(data, 0, data.Length);

responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bool flag = responseData.Contains("Your Connection with API Server is Successful");

if (!flag)

throw new NotConnectedException();

return flag;

}

You can catch response in two ways:

1- Response code: <code>1000</code>, an example for successful attempt

2- Response message :<msg>Server response</msg>

I used msg tag to make my program more readable.

I also wrote some exception classes to response to errors properly. As you can see “NotConnectedException” class is created to throw an exception in the case of connection failure.

Login:

In order to login to API you should specify your credential information. In code below you can make an XML command and pass it to the server.

There is a hashed value called checksum which exists in all of commands either you send to API or receive from it. This is for security purposes to prevent anyone from intersecting the connection and changing the value of the fields. Each command such as login or Domain registration has its own checksum format. Here as you cans see below it is the concatenation of username, hashed password, a new GUID and a string contains “login”. You then should hash it.

To hash your password you can click on this.

bool Login()

{

string chksum = "";

string HashedPass = getMd5Hash(Password);

Guid guid = Guid.NewGuid();

// MemberID is your username and for you password you should hash you password

chksum = MemberID + HashedPass + guid.ToString() + "login";

chksum = getMd5Hash(chksum);

StringBuilder sb = new StringBuilder();

sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");

sb.Append("<request>");

sb.Append("<category>client</category>");

sb.Append("<action>Login</action>");

sb.Append("<params>");

sb.Append("<param name=\"clid\">" + MemberID + "</param>");

sb.Append("</params>");

sb.Append("<cltrid>" + guid.ToString() + "</cltrid>");

sb.Append("<chksum>" + chksum + "</chksum>");

sb.Append("</request>");

String responseData = String.Empty;

// Buffer to store the response bytes.

Byte[] data;

data = System.Text.Encoding.ASCII.GetBytes(sb.ToString());

// Send the message to the connected TcpServer.

stream.Write(data, 0, data.Length);

//Reads data and returns it to the application

Int32 bytes = stream.Read(data, 0, data.Length);

responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bool flag = responseData.Contains("Command completed successfully");

if (!flag)

throw new NotLoginException();

return flag;

}

Log out:

bool Logout()

{

string chksum = "";

string HashedPass = getMd5Hash(Password);

Guid guid = Guid.NewGuid();

chksum = MemberID + HashedPass + guid.ToString() + "logout";

chksum = getMd5Hash(chksum);

StringBuilder sb = new StringBuilder();

sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
sb.Append("<request>");
sb.Append("<category>client</category>");
sb.Append("<action>Logout</action>");
sb.Append("<params>");
sb.Append("<param name=\"clid\">" + MemberID + "</param>");
sb.Append("</params>");
sb.Append("<cltrid>" + guid.ToString() + "</cltrid>");
sb.Append("<chksum>" + chksum + "</chksum>");
sb.Append("</request>");
String responseData = String.Empty;

// Buffer to store the response bytes.

Byte[] data;

data = System.Text.Encoding.ASCII.GetBytes(sb.ToString());

// Send the message to the connected TcpServer.

stream.Write(data, 0, data.Length);

//Reads data and returns it to the application

Int32 bytes = stream.Read(data, 0, data.Length);

responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bytes = stream.Read(data, 0, data.Length);

responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);

bool flag = responseData.Contains("Command completed successfully");

return flag;

}