Friday, May 23, 2008
Bolggers need this
For me, all the above is just true. AS am using blogger I faced that problem when trying to paste some c# code into one of my posts.The problem came from the html validation that blogger makes.So whenever you have some html character like "<,> and &" you will need to html encode them otherwise you will get an error. Postable is a simple site allowing you to paste your code and generates a friendly output code by converting all the special characters to their HTML Encodes. After converting your code using Postable you would probably use the <code> tag to distinguish your post text from your code.
Saturday, May 3, 2008
Kayak != Kayak API
Anyway, since I spent a couple of hours traying to get familiar with the system I decided to share it with you.
The API is so simple, just send few GET HttpRequest and Handle the Xml response.The squencve is as follows:
- Send request with the developer key to get the sessionID.
- Make another request using that sessionID to get the searchID.
- Make last request using both sessionID, searchID and search criteria in the query string.
The Url for each request and query string paramaters are documented in the API Specs .
Here is a simple page with a gridview and a button to grab some data using this API. I didn't consider error handling in the code since you all can do. This is a very simple implementation for the API to be able to get started.
You will need to change the DEVELOPERKEY in the first url with the key you get from the site.
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net;
using System.IO;
using System.Xml;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string xmlResult = MakeRequst("http://api.kayak.com/k/ident/apisession?token=YOUR TOKEN");
string sid = GetRequiredId(xmlResult, "sid");
xmlResult = MakeRequst("http://www.kayak.com/s/apisearch?basicmode=true&oneway=n&origin=BOS&destination=SFO&destcode=&depart_date=05/09/2008&depart_time=a&return_date=05/13/2008&return_time=a&travelers=2&cabin=b&action=doflights&apimode=1&_sid_=" sid);
string searchId = GetRequiredId(xmlResult, "searchid");
xmlResult = MakeRequst("http://www.kayak.com/s/basic/flight?searchid=" searchId "&c=10&apimode=1&_sid_=" sid);
List<Leg> legs = GetAllLegs(xmlResult);
if (legs != null)
{
GridView1.DataSource = legs;
GridView1.DataBind();
}
}
//Get sessionId and searchId
private string GetRequiredId(string xml, string idName)
{
string id = "";
XmlDataDocument doc = new XmlDataDocument();
doc.LoadXml(xml);
XmlNodeList nodes = doc.GetElementsByTagName(idName);
if (nodes != null && nodes.Count > 0)
id = nodes[0].InnerText;
return id;
}
//Make required request
private string MakeRequst(string requestUrl)
{
System.Net.HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string xmlText = reader.ReadToEnd();
return xmlText;
}
//Get a list of Leg objects.
private List<Leg> GetAllLegs(string responseXml)
{
XmlDataDocument doc = new XmlDataDocument();
doc.LoadXml(responseXml);
List<Leg> legs = new List<Leg>();
XmlNodeList legNodes = doc.GetElementsByTagName("leg");
foreach (XmlNode legNode in legNodes)
{
Leg leg = new Leg();
for(int i=0;i<legNode.ChildNodes.Count;i )
{
if (legNode.ChildNodes[i].Name == "airline_display")
leg.AirLine = legNode.ChildNodes[i].InnerText;
else if (legNode.ChildNodes[i].Name == "orig")
leg.Origin = legNode.ChildNodes[i].InnerText;
else if (legNode.ChildNodes[i].Name == "dest")
leg.Dest = legNode.ChildNodes[i].InnerText;
else if (legNode.ChildNodes[i].Name == "depart")
leg.Depart = Convert.ToDateTime(legNode.ChildNodes[i].InnerText);
else if (legNode.ChildNodes[i].Name == "arrive")
leg.Arrive = Convert.ToDateTime(legNode.ChildNodes[i].InnerText);
else if (legNode.ChildNodes[i].Name == "stops")
leg.Stops = Convert.ToInt32(legNode.ChildNodes[i].InnerText);
else if (legNode.ChildNodes[i].Name == "duration_minutes")
leg.Duration = Convert.ToInt32(legNode.ChildNodes[i].InnerText);
else if (legNode.ChildNodes[i].Name == "cabin")
leg.Cabin = legNode.ChildNodes[i].InnerText;
}
legs.Add(leg);
}
return legs;
}
}
// Leg class represents part of the returned XML
class Leg
{
private string airLine, origin, dest, cabin;
int stops, duration;
private DateTime depart, arrive;
public string AirLine
{
get { return airLine; } set { airLine = value; }
}
public string Origin
{
get { return origin; } set { origin = value; }
}
public string Dest
{
get { return dest; } set { dest = value; }
}
public string Cabin
{
get { return cabin; } set { cabin = value; }
}
public int Stops
{
get { return stops; } set { stops = value; }
}
public int Duration
{
get { return duration; } set { duration = value; }
}
public DateTime Depart
{
get { return depart; } set { depart = value; }
}
public DateTime Arrive
{
get { return arrive; }
set { arrive = value; }
}
}
Friday, May 2, 2008
Godaddy + Windows + IIS7 = PHP
Regards,