Archive for April 4th, 2008

Intergrating with google Search API in .NET?

Friday, April 4th, 2008

Google created a search API web service that allow us to search billions of web pages from our own applications, those web services can be exposed from any programming environment such as .Net or java.
So the following are the needed steps for integrating with this service and small .Net sample .
1. You can download the Developers Kit which contains .net and java samples and the wsdl file from the following Url.
http://www.google.com/apis/download.html
2. You must have google account ‘gmail’ in order to get the license key for using this API. So if you don’t have an account you should go to the following link and create one.
https://www.google.com/accounts/NewAccount?
continue=http://api.google.com/createkey&
followup=http://api.google.com/createkey
or if you already have one just sign in and an email message containing the license key will be sent to you .
https://www.google.com/accounts/Login?continue=

http%3A%2F%2Fapi.google.com%2Fcreatekey&followup=

http%3A%2F%2Fapi.google.com%2Fcreatekey
3. After you get the license key you can start develop the application which we will mention in the following steps.
* Open Visual Studio .Net 2003 or 2005
* Choose new project and select the type and your preferred language ‘ we will choose win app with C# in our example’
* Right click in the solution explorer and choose add web reference and write the following url in the appeared dialog
http://api.google.com/GoogleSearch.wsdl
# Put in the form a textbox and change its ID to Txt_Text for the search and put a button and change it’s ID to Btn_Search
# Drag and drop a datagrid if you are using 2003 or datagridview if 2005 .
# Write the following code under the Btn_Search _Click event.
com.google.api.GoogleSearchService s = new TestGoogle.com.google.api.GoogleSearchService();
com.google.api.GoogleSearchResult r = s.doGoogleSearch(”put your lisence key her “, Txt_Text.Text, 0, 10, false, “”, true, “”, “”, “”);
int estimatedCount =r.estimatedTotalResultsCount;
DataTable dtResults = new DataTable();
dtResults.Columns.Add(new DataColumn(”Title”, typeof(string)));
dtResults.Columns.Add(new DataColumn(”Summary”, typeof(string)));
dtResults.Columns.Add(new DataColumn(”URL”, typeof(string)));
for (int i = 0; i < 10; i++)
{
DataRow dr = dtResults.NewRow();
dr[0] = r.resultElements[i].title;
dr[1] = r.resultElements[i].snippet;
dr[2] = r.resultElements[i].URL;
dtResults.Rows.Add(dr);
}
dataGridView1.DataSource = dtResults;

Posted by Mahesh ( Tryangled )

Using SOAP Headers to validate clients

Friday, April 4th, 2008

how you can secure your web service by using SOAP headers.Often times we create a web service that we only want to enable for our own clients or would like to enable only for authorized clients. You can accomplish this by using SOAP headers to authenticate the client and process the request or throw an exception when they are not properly authorized.
I will begin by defining a simple web service with one method named: HelloWorld. Please find the code below:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = “http://tryangled.com/”)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public ValidationSoapHeader Authentication;
private const string DEV_TOKEN = “12345″;
public Service()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[SoapHeader(”Authentication”)]
[WebMethod]
public string HelloWorld()
{
if (Authentication != null && Authentication.DevToken == DEV_TOKEN)
{
return “Hello World”;
}
else
{
throw new Exception(”Authentication Failed”);
}
}
}

You will see I have add the Attribute [SoapHeader(”Authentication”)] which will add the SOAP header as part of that method. In addition within the body of the method you will see the check:
if (Authentication != null && Authentication.DevToken == DEV_TOKEN)
this will check to see if the token being passed in from the client matches the once defined in the service. We have to check for null as well since the .net 2.0 will not require the headers.  You could obviously take this to a more granular level and define a token for each user or a token defined in the web.config. If the token does match then process the method and return “Hello World’. If the token does not match then throw an exception.

Please see the ValidationSoapHeader class defined below:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services.Protocols;
public class ValidationSoapHeader : SoapHeader
{
private string _devToken;
public ValidationSoapHeader()
{
}
public ValidationSoapHeader(string devToken)
{
this._devToken = devToken;
}
public string DevToken
{
get { return this._devToken; }
set { this._devToken = value; }
}
}
As for the client side. I created a simple console application and added the web service as a reference in the project.

localhost.ValidationSoapHeader header = new ConsoleMyCsharpClient.localhost.ValidationSoapHeader();
header.DevToken = “12345″;
localhost.Service ws = new ConsoleMyCsharpClient.localhost.Service();
ws.ValidationSoapHeaderValue = header;
Console.WriteLine(ws.HelloWorld());
Console.ReadLine();

Posted by Mahesh ( Tryangled )

Creating a Custom Event in ASP.NET?

Friday, April 4th, 2008

In order to create a custom event type, you have to derive your own event class from one of the derivable Web Event base classes. You can then create and raise instances of this event at runtime, and configure subscriptions to it as described above.

In the example below, a custom event is created with a custom event code and message and an instance of this event is raised in a page event handler:

// in the code directory, or your application source code
class MySampleEvent : System.Web.Management.WebBaseEvent
{
public const int MySampleEventCode = 200001;

public MySampleEvent(String message) : base(message, null, MySampleEventCode) {}
}
// in an aspx page
void Page_Load()
{
// create an instance of the event
MySampleEvent e = new MySampleEvent(”In Page_Load()”);
// raise the event to the web event engine
e.Raise();
}

Posted by Mahesh ( Tryangled )