Boxing and Unboxing (A key reason to use Generic)

April 7, 2011 2 comments

This article is a discussion about boxing and unboxing in .Net and also the ways to overcome it.

This term is mostly used when we want to convert value types to reference types or vice versa. We really should care about it in development of critical systems, web applications with high traffic or using classes or methods with frequent use.

Boxing: This is the process of converting the value of a value type variable to a reference type variable.

Assume that you want to store a user-defined type (struct) into an ArrayList (As you know user-defined/struct types are value type). Now let’s know what is stored in this ArrayList?

If you take a look at the Add() method of ArrayList, it has only one overload for this method which is Add(Object object) [and Object is a reference type]. It is good news due to the possibility of storing totally different types into each index of an ArrayList. However, it has some bad news too because it needs to convert your value type variable to a reference type variable. It means that it needs a boxing to fulfill this need.

struct TypeA

{

public int VarA;

public int VarB;

public TypeA(int VarA, int VarB)

{

this.VarA = VarA;

this.VarB = VarB;

}

}

public void TestBoxingAndUnBoxing()

{

TypeA typeA = new TypeA(10, 20);

System.Collections.ArrayList arrList = new System.Collections.ArrayList();

//Boxing occures here 

arrList.Add(typeA);

}

What Happens when a boxing occurs:

1- Because value-typed variables are stored in stack, a new memory cell in heap must be allocated to store this object. Reference types use heap (dynamic memory space) to be stored.

2- The value type’s bits are copied from stack to their new location in heap.

3- The new address is returned to be stored in ArrayList. So we now have a reference type instead of a value type.

Therefore, boxing can be very expensive and sometimes harmful.

Unboxing: is, of course, exactly opposite to boxing, it is the time when a reference type is converted to a value type.

After storing value types into ArrayList, they are wanted to be used somewhere. In other words, you should convert them to their original type in order to use them.

typeA = (TypeA)arrList[0];

System.Console.Write(typeA.VarB.ToString());

What happens in unboxing?

1- It Checks whether it is null or not, then it checks whether the specified type is convertible or not. If both of the above conditions fail it returns an exception.

2- The reference of the object inside the ArrayList is returned. Then bits are copied into the value typed variable.

It has some penalties again. To overcome this problem .Net suggests using Generics when time is totally eminent (But it is a better to consider it always).

Generics is out of scope of this article; however, I describe it briefly in a sample code to show its power and its immense difference compared with objects.

Generics classes/methods are similar to type-less ones but they are not. You can specify their types before compile time when you are coding. For example for classes their types must be specified during their instantiation. Therefore, it gives you the flexibility to have different types in each instance of a class.

Code below is a comparison between a Generic class and an Object one.

//T and U are generic types. It means that you can specify their types in class instantiation. So

class GenericClass
{
public T t;
public U u;

 public GenericClass(T t, U u)
{
this.t = t;
this.u = u;
}
}

//An object class that may cause boxing and unboxing during run-time
class ObjectClass
{
public object t;
public object u;

 

 public ObjectClass(object t, object u)
{
this.t = t;
this.u = u;
}
}

Now I want to show you the time it takes to work with each one of them for ten million iterations.

protected void Page_Load(object sender, EventArgs e)
{
DateTime dt1 = DateTime.Now;
ObjectClass ObjClass;
for (int i = 0; i < 10000000; i++)
{
ObjClass = new ObjectClass(10, "Peace");
}
DateTime dt2 = DateTime.Now;

TimeSpan tsDef = dt2.Subtract(dt1);
Response.Write("Object: " + tsDef.TotalMilliseconds.ToString() + "
");

 dt1 = DateTime.Now;
GenericClass GenClass;
for (int i = 0; i < 10000000; i++)
{
GenClass = new GenericClass(10, "Peace");
}
dt2 = DateTime.Now;
tsDef = dt2.Subtract(dt1);
Response.Write("Generic: " + tsDef.TotalMilliseconds.ToString());
}
Boxing and Unboxing

Fig 1, the result of comparision

The result clearly shows this difference. (Of course, you get different results each time you run the program but the ratio remains almost the same)

As you see, it is near to two times faster by using the Generic types.

Reference: Jeffrey Richter, MSPress: MCTS Self-Paced Training Kit (Exam 70-536)

JQuery Autocomplete with ASP.Net

March 29, 2011 4 comments

In this article I want to describe combination of Autocomplete of JQuery and ASP.Net web service. Up to now I really couldn’t find a good article about this issue so it can be useful in the web.

Autocomplete is a plugin of JQuery that you can be used to implement autocomplete with the least efforts. You can find it here.

JQuery Autocomplete plugin has a property called source, in Ajax based programming you should change its source regarding what is entered into the textbox. Assume that you enter “ala” after that a callback is generated that sends some parameters to the web service (textbox.text and other parameters that you want) then in the response data you can feed source property of Autocomplete plugin. I think this stage can be your unique part of your job.

AutoComplete

Fig 1

In this article I’ll discuss about generating a new callback on typing each new word and how to assign the returned value with the source property of Autocomplete plugin. You then can change the web service section with your desired code such as connecting to database and retrieving data on what is sent as parameter to the web service.

Web Service:

I start with web service because it is really simple.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Xml;

using System.Text;

using System.Web.Script.Services;

namespace ArticleDean.web.Service

{

/// <summary>

/// Summary description for WSDataService

/// </summary>

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.ComponentModel.ToolboxItem(false)]

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

[ScriptService]

public class WSDataService : System.Web.Services.WebService

{

[WebMethod]

public string GetStates(string typedValue, string test)

{

test = "ALABAMA:ALASKA:AMERICAN SAMOA:ARIZONA:ARKANSAS:CALIFORNIA:COLORADO:CONNECTICUT:DELAWARE:DISTRICT OF COLUMBIA:FEDERATED STATES OF MICRONESIA:FLORIDA:GEORGIA:GUAM:HAWAII:IDAHO:ILLINOIS:INDIANA:IOWA:KANSAS:KENTUCKY:LOUISIANA:MAINE:MARSHALL ISLANDS:MARYLAND:MASSACHUSETTS:MICHIGAN:MINNESOTA:MISSISSIPPI:MISSOURI:MONTANA:NEBRASKA:NEVADA:NEW HAMPSHIRE:NEW JERSEY:NEW MEXICO:NEW YORK:NORTH CAROLINA:NORTH DAKOTA:NORTHERN MARIANA ISLANDS:OHIO:OKLAHOMA:OREGON:PALAU:PENNSYLVANIA:PUERTO RICO:RHODE ISLAND:SOUTH CAROLINA:SOUTH DAKOTA:TENNESSEE:TEXAS:UTAH:VERMONT:VIRGIN ISLANDS:VIRGINIA:WASHINGTON:WEST VIRGINIA:WISCONSIN:WYOMING";

return test;

}

}

}

The first parameter that web service gets is typedValue. This parameter returns the typed value in textbox. The second parameter is a test one; it is just to show how to send more than one parameter in JSON datatype. You can use them to retrieve your desired data from database but here we just return a long text with ‘:’ separator (This is for training purpose).

ASPX file:

Sorry that it is Image, because of the problem of wordpress to accept html and aspx code, I have to publish this section in image

In the front of the source, there is a function that returns the callback result. It has two parameters, the second one is a method called response (this is a key note) that you should pass the resulting data from callback to it.

In data section of Ajax, I wrote a JSON data, JSON is a pair of key, value that is separated by a ‘:’.

$(".QueryText").val()

returns the typed words in text box

In success section, I splited the resulting string with ‘:’ character and assigned it an array (because JavaScript is a dynamic type checker you don’t need to specify the variable as array, it detects it automatically). Then I sent it to the response method as parameter. This is the only way to return data to the source property of Autocomplete. [return] keyword can’t do this (I wasted a lot of my time to return a value from return keyword, so you can save yours).

Among Autocomplete properties I’m going to discuss some of them. First delay, this parameter is to postpone the callback request to the server. It is very important to do this because ignoring it causes a lot of overhead on the server (it would be a self Denial of Service attack :D). The next is minLength, it depicts that your Autocomplete sends the request after typing a specific number of characters. It also reduces the overhead if you increase it to a meaningful number according to your need.

In header of your form I included JQuery file (jquery-1.5.1.min.js), Autocomplete plugin file (jquery-ui-1.8.custom.min.js) this is a customized file; you can generate it at this address. And also a CSS file that is the official released theme for Autocomplete; you can download and edit it.

Should you have any questions comment me.

Love and peace for you all.

MS Chart with C#

March 27, 2011 8 comments

In this article I’d like to describe MS Chart (.Net 4.0) through an example. The picture below is the final result.

MS Chart

Fig 1

Initialization:

1-      Create a temp folder to store temp image files (c:\TempImageFiles) and add some codes to your web config file.
<compilation debug="true" targetFramework="4.0">

<assemblies>

<add assembly=”System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″/>

</assemblies>

</compilation>

<appSettings>

<add key=”ChartImageHandler” value=”storage=file;timeout=20;dir=c:\TempImageFiles\”/>

</appSettings>

<system.web>

<httpHandlers>

<add path=”ChartImg.axd” verb=”GET,HEAD,POST” type=”System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ validate=”false” />

</httpHandlers>

</system.web>
In .Net 4.0 edition you don’t need to download and install any control to use MSChart, you just need to drag and drop it from Data section of MS Visual Studio 2010 toolbox to your form (But I really dislike doing this, it’s better to write code in source tab of your form). In order to code it, add System.Web.UI.DataVisualization.Charting namespace to your form.

In this sample I will go through coding MSChart at runtime. Once you learned this, you learned static one also.

Start Coding:

ASPX Form:

Add this code to your form, this is the only thing you need to have in your aspx form:

<asp:Chart ID="MSChart" runat="server">

</asp:Chart>

Code Behind:

Below codes are clear, you just add them in your cs file:

MSChart.Width = 900;

MSChart.Height = 600;

//It renders chart as an image and places it into an &lt;img&gt; html tag.

MSChart.RenderType = RenderType.ImageTag;

//Adds title to chart

Title t = new Title("Sample Chart", Docking.Top, new System.Drawing.Font("Tahoma", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));

MSChart.Titles.Add(t);

Chart Sections:

MS Chart has two primary sections, Series and CharArea. You can either have a design time data in these two sections or add data programmatically during runtime.

–          Series are the most important part of each chart. Every chart has at least one series. A chart is a series of numbers which can be plotted into space as X and Y points. These numbers are dependent and depict a concept together regarding an issue. Assume that this series build a sine shape line, so this sine shape line is a series then you can add another series in the same chart to depict another meaning. These two series can be used to compare two different data of the same concept.

Fig 2

Fig 3

–          ChartArea: Is a place that you want to plot your series (so it contains series). It is a collection; therefore your chart can contain multiple ChartAreas.

MSChart.Series.Add("Series1");

MSChart.Series["Series1"].ChartType = SeriesChartType.Column;

MSChart.Series["Series1"].Color = System.Drawing.Color.Goldenrod;

MSChart.Series["Series1"].YValueType = ChartValueType.Int32;

Series1 is added to the collection of MSChart.Series. Each series has some important properties. ChartType that specifies the type of chart which can be Column (Same as ours), Pie, Doughnut etc. Color is to specify color of series and YValueType is the type of values in Y axis.

MSChart.ChartAreas.Add("ChartMainArea");

After defining Series you should define ChartArea. You can easily add a chart area to the CharAreas collection.

Legends:


Legend leg = new Legend("Legend1");

MSChart.Legends.Add(leg);

MSChart.Legends[0].Alignment = System.Drawing.StringAlignment.Near;

In order to describe each series in a chart, Legends are used. To add them you can write the above code. Each ChartArea needs a legend; it automatically detects your series in a ChartArea.

Now it’s time to add data to our series:

Random _r = new Random();
for (int i = 10; i &lt; 100; i+=10)

{
int n = _r.Next(0, 100);
MSChart.Series["Series1"].Points.AddXY(i, n);

}
Points.AddXY(x,y)

is a fast method to specify data of each point directly.

Up to now the below picture is created.

MS Chart

Fig 4

Theming and Appearance:

MSChart has some appearance properties that help your chart look more attractive. (Since chart is only for reporting purpose, either management or customer will see it, so make it the most beautiful :D, don’t forget the power of feelings).

You have definitely seen 3D charts. We want to develop one.

ChartArea3DStyle chartArea3DStyle = new ChartArea3DStyle();

chartArea3DStyle.Enable3D = true;

chartArea3DStyle.LightStyle = LightStyle.Realistic;

chartArea3DStyle.Rotation = 5;

chartArea3DStyle.Inclination = 40;

chartArea3DStyle.PointDepth = 50;

MSChart.ChartAreas["ChartMainArea"].Area3DStyle = chartArea3DStyle;

ChartArea3DStyle class has some properties to style a 3D chart. Then Area3DStyle property should be assigned with a ChartArea3DStyle object. After these simple changes you’ll have the below picture.

MS Chart

Fig 5

Adding more series:

It is really easy to add more series. Just write the following code:

MSChart.Series.Add("Series2");

MSChart.Series["Series2"].ChartType = SeriesChartType.Column;

MSChart.Series["Series2"].BorderWidth = 2;

MSChart.Series["Series2"].YValueType = ChartValueType.Int32;

for (int i = 15; i <= 100; i += 10)

{

MSChart.Series["Series2"].Points.AddXY(i, i);

}
MS Chart

Fig 6

It adds a new series and adds some points to it.

Wish you enjoyed it.

Peace and love for you all

References:

http://www.datadynamics.com/Help/ActiveReports6/arCONChartSeries.html

http://archive.msdn.microsoft.com/mschart

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”.

/// &lt;summary&gt;
/// 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;

}

IT is business

February 27, 2011 Leave a comment

21st century is called “Information Era”. After invention of computer in the previous century, pioneers felt the demand of connecting this new invention to one another in order to construct a chain of connected datastores that made so-called “Internet”. This happening changed the life style of many traditional sciences from Medical to finance. One of the most prominent impact of this change occurred in marketing. Internet-based businesses try to have the most possible share of this media.

According to the statistics, 28 percent of people can connect to the internet worldwide which are roughly 1,932,112,000. Marketers try to find solutions to attract more people to their business. There is a good question in their minds: “How many people can interact to my business directly?” honestly no media can bring as many users as internet can bring to your business.

Nowadays, online businesses are so popular because of many advantages. They can count on people access worldwide without any limitation. The term “24, 7, 365” is used a lot to showoff their online business. Online transactions let they feel relaxed and secured while doing business with their customers. Their customers also can access their intelligent services all the time; therefore, they can also take the same advantages as business owners can. For customers it is easier to refer to online services and use them rather than going physically and engage themselves.

On the other hand, managers and stack holders of businesses prefer to track their jobs online. Assume that you have a multinational business. It is near to impossible to know what is going on in all of your offices in the world. Internet tracking services are the best solutions to make you an offshore manager; it helps you track all of you job, employees, tasks and customers online in a pleasure trip.

There are many factors engaged with this issue in order to make a successful business chain. Learning more about online potentials help you most to boot your business in this highly competitive world.

 

Categories: General