Bookmark and Share

Monday, November 08, 2010

How to call Outside.in location service using c#

If you are evaluating the Outside.in localized news service you may find useful this little command line program, it generates the right MD5 security code and it calls the "Location by name" service:

Here a basic test program written in c#, I think is a good start, it queries for locations:

///////////////////

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Security.Cryptography;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter location,for example new york:");
string action = Console.ReadLine();
while (action != "")
{
string search;
search = action;

string mykey = "xxx";
string shared_secret = "xxx";
int unixTime = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
string sUrl = "http://hyperlocal-api.outside.in/v1.1/locations/named/" + search + "?dev_key=" + mykey + "&sig=" + MD5_ComputeHexaHash(mykey + shared_secret + unixTime.ToString());

WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sUrl);

Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();

StreamReader objReader = new StreamReader(objStream);

string sLine = "";
int i = 0;

while (sLine != null)
{
i++;
sLine = objReader.ReadLine();
if (sLine != null)
Console.WriteLine("{0}:{1}", i, sLine);
}
action = Console.ReadLine();
}
}

public static string MD5_ComputeHexaHash(string text)
{
// Gets the MD5 hash for text
MD5 md5 = new MD5CryptoServiceProvider();
byte[] data = Encoding.Default.GetBytes(text);
byte[] hash = md5.ComputeHash(data);
// Transforms as hexa
string hexaHash = "";
foreach (byte b in hash)
{
hexaHash += String.Format("{0:x2}", b);
}
// Returns MD5 hexa hash
return hexaHash;
}
}
}