Google Search

Thursday, June 26, 2008

Consuming HTTPS web service using c# and asp.net

I have been trying this for this for the whole past week and finally got a solution, what I was provided was a https wsdl file and had to consume it. I tried a lot in ASP and could not find any solution and came up with consuming the web service using .NET 2.0 using C# and asp.net

The webservice provided was something like this:
https://xx.xxx.xxx.xxx/test/services/sum?wsdl"

The main issue was to handle the certificates, if we use https.

Suppose you have a function sum which returns sum of two numbers if provided as parameters in the wsdl definition.

To start with create a new website in Visual Studio (select Framework 2.0 if using VS 2008).
Create a web reference. Go to Project>>Add Web Reference.
A new Window will open here give the https link i.e https://xx.xxx.xxx.xxx/test/services/sum?wsdl and press the GO button. You will get those Certificate pop ups, select OK and accept then you can see the all the function in the description.
Provide a name let it be "web.sumref" and click on AddReference button.
Now go to default.aspx.cs file.
Here add the namespace "using web.sumref"
To resolve the issue with Certificates you need to implement the a class that impements ICertificatePolicy interface. Using this class we can return true so that it accepts all certificates.
For this you need to use the namespace:
using System.Security.Cryptography.X509Certificates;
And you need to call the CheckValidationResult before you call the web service.

Here is the code for default.aspx.cs:

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 web.sumref;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Threading;
using System.IO;
using System.Text;
using System.Xml;
public partial class _Default : System.Web.UI.Page {

//create object for web service
SumWebService obj = new SumWebService();
protected void Page_Load(object sender, EventArgs e) {
ServicePointManager.CertificatePolicy = new MyCertificateValidation();
int sum=obj.sum(2,3);
Response.Write(sum.ToString());
}
public class MyCertificateValidation : System.Net.ICertificatePolicy{
//This class handles problems with certificates if ssl (https) is used
bool ICertificatePolicy.CheckValidationResult(ServicePoint srvPoint, X509Certificate cert, WebRequest request, int problem) {

return true;
// Accept all certificates
}
}

Wednesday, May 21, 2008

Working with Stored Procedure in ASP.NET

This article is regarding working with stored procedure, i.e call a stored procedure from a web application. Here we will call a stored procedure to insert data to one of the table in the database.
Step 1: Creating the stored procedure
I have SQL Express 2005 installed, you may need to download the SQL server Management Studio express from microsoft website. Open the Management Studio then create a new database lets say "test". Then create a new Table called "login" with the following columns:

id (primary key) varchar(50)

password varchar(50)

email varchar(50)

No go to "Programmability">>"Stored Procedure">>right click>>"New Stored Procedure"

Here following is the stored procedure to insert values to the table we just created:


CREATE PROCEDURE submitrecord
(
@id varchar(50),
@password varchar(50),
@email varchar(50)
)
AS
insert into login (id, password,email)values (@id,@password,@email)

and save it.


Step 2: Execute the stored procedure from web application
From the Visual Studio create a new Web Application let it be "storedproc"
Design the default.aspx as following:
Now in the default.aspx.cs should look like this:


using System;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
DataSet ds = new DataSet();
SqlConnection con;
SqlCommand cmd = new SqlCommand();
SqlParameter sp1 = new SqlParameter();
SqlParameter sp2 = new SqlParameter();
SqlParameter sp3 = new SqlParameter();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con = new SqlConnection("Trusted_Connection=Yes;database=test;data source=.\SQLEXPRESS;");
con.Open();
cmd = new SqlCommand("submitrecord", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = txtId.Text;
cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = txtpwd.Text;
cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = txtemail.Text;
cmd.ExecuteNonQuery();
con.Close();
}
}


The code is self explainatory, and the main issue any person faces is with the connection string.

Tuesday, April 1, 2008

Ajax AutoCompleteExtender using Google Suggest API

Hi,

After a long time I am posting an article.

This is on the Ajax toolkits control called AutoCompleteExtender.

Here I am trying to make a google suggest like site, using the Google Suggest REST API.

The Page will somewhat look like this:



The autocomplete extender works on the textbox and uses a webservice method. You need to have a webform (default.aspx), a webservice (google.asmx). Follwoing is the default.aspx page:
NOTE: I have used VS 2005 with AJAX extention and AjaxControlToolkit. Create a new Ajax enabled website from VS 2005. The dfault.aspx has nothing but 1 textbox, and the associated autocompleteextender:

Google Suggest:
asp:TextBox ID="TextBox1" runat="server"
asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click"
cc1:AutoCompleteExtender
ID="AutoCompleteExtender1"
runat="server"
Enabled="true"
MinimumPrefixLength="1"
TargetControlID="TextBox1"
ServicePath="google.asmx"
ServiceMethod="getList"
/cc1:AutoCompleteExtender
______________________________default.aspx___________________________



____________________________________________________________________ You may give the code for Button1_Click:

protected void Button1_Click(object sender, EventArgs e)
{
string url = "http://www.google.co.in/search?hl=en&q=" + TextBox1.Text;
Response.Redirect(url);
}
____________________________________________________________
google.asmx has one method called the getList which returns an array fetched from the google suggest API.

_______________________google.asmx____________________________________


using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;

///


/// Summary description for google
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class google : System.Web.Services.WebService
{
[WebMethod]
public string[] getList(String prefixText)
{
XmlDocument doc = new XmlDocument();
List suggArList = new List();
string url = "http://google.com/complete/search?output=toolbar&q=" + prefixText;
doc.Load(url);
foreach (XmlNode node in doc.SelectNodes("//CompleteSuggestion"))
{
string value = node.SelectSingleNode("suggestion/@data").InnerText;
suggArList.Add(value);
}
return suggArList.ToArray();
}
}
____________________________________________________________________

NOTE: there seems to be an issue when code behind system is used for the webservice, also we need to ensure that [System.Web.Script.Services.ScriptService] is called.