Archive for May, 2011

Send an SMS from Windows Phone 7 (WP7/Silverlight)

This is a simple piece of code that allows you send an SMS from Windows Phone 7 with Silverlight
The XAML is as follows

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="29,35,0,0" Name="lblFromName" Text="From (Name)" VerticalAlignment="Top" />
            <TextBox Height="80" HorizontalAlignment="Left" Margin="170,6,0,0" Name="tbFromName" Text="" VerticalAlignment="Top" Width="259" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="29,112,0,0" Name="lblFromNumber" Text="From (Number)" VerticalAlignment="Top" />
            <TextBox Height="80" HorizontalAlignment="Left" Margin="170,92,0,0" Name="tbFromNumber" Text="00" VerticalAlignment="Top" Width="259" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="29,205,0,0" Name="lblToName" Text="To (Name)" VerticalAlignment="Top" />
            <TextBox Height="80" HorizontalAlignment="Left" Margin="170,178,0,0" Name="tbToName" Text="" VerticalAlignment="Top" Width="259" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="33,289,0,0" Name="lblToNumber" Text="To (Number)" VerticalAlignment="Top" />
            <TextBox Height="80" HorizontalAlignment="Left" Margin="170,264,0,0" Name="tbToNumber" Text="00" VerticalAlignment="Top" Width="259" />
            <Button Content="Send SMS" Height="93" HorizontalAlignment="Left" Margin="170,570,0,0" Name="btnSendSMS" VerticalAlignment="Top" Width="239" Click="btnSendSMS_Click" />
            <TextBlock Height="44" HorizontalAlignment="Left" Margin="31,369,0,0" Name="textBlock1" Text="Message" VerticalAlignment="Top" Width="199" />
            <TextBox Height="166" HorizontalAlignment="Left" Margin="33,397,0,0" Name="tbMessage" Text="" VerticalAlignment="Top" Width="374" TextChanged="tbMessage_TextChanged" />

Then the code behind btnSendSMS_Click is

     private void btnSendSMS_Click(object sender, RoutedEventArgs e)
            var strFromName = tbFromName.Text;
            var strToName = tbToName.Text;
            var strFromNumber = tbFromNumber.Text;
            var strToNumber = tbToNumber.Text;
            var strMessage = tbMessage.Text;
            // Validation
            if (!strToNumber.StartsWith("00") || !strFromNumber.StartsWith("00"))
                MessageBox.Show("Mobile numbers must be written in international format, for example 0044 for the UK, followed by the mobile phone number, without the first zero");
            if (strFromName.Length  11)
                MessageBox.Show("Use your real name or else the reciepient may not recognize the sender, your name should be between 3 and 11 letters long");
            if (strFromNumber == strToNumber)
                MessageBox.Show("You cannot send a text message to yourself");
            if (strMessage.Length < 3)
                MessageBox.Show("Your message is blank, please type a message");
            sendSmsSoapClient sms = new sendSmsSoapClient();
            sms.SendSmsCompleted +=
                new EventHandler(sms_SendSmsCompleted);
                sms.SendSmsAsync(strFromName, strFromNumber, strToNumber, strMessage, "xx-xx");
            catch (Exception ex)
                MessageBox.Show("SMS failed to send due to: " + ex.Message);

        void sms_SendSmsCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
            if (e.Error == null)
                MessageBox.Show("SMS Sent successfully");
                MessageBox.Show("SMS not sent due to " + e.Error.Message);

Then you need a web service reference to called “webservice”

Tip: If you use AffiliateSendSMS API call rather than SendSMS, then you can get paid via your affiliate account on FreebieSMS! 🙂

Categories: Uncategorized

A HTTP Proxy in Python for Google AppEngine

Labnol’s AppEngine Proxy is useful for viewing other pages requested via Google AppEngine, however, it does change the HTML, and doesn’t handle POST data. This is a code example in Google AppEngine Python showing how to implement a HTTP proxy in a similar way to Labnol.

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

import urllib2
import urllib
import re
import array
import urllib2

class MainHandler(webapp.RequestHandler):
	def get(self):	
		#self.response.out.write('[GET] The URL Requested was ' + self.request.query_string)
		response =
		html =

	def post(self):	
		#self.response.out.write('[POST] The URL Requested was ' + self.request.query_string + "
") args = self.request.arguments() strPostdata = "" for arg in args: strPostdata = strPostdata + arg + "=" + self.request.get(arg) + "&" #self.response.out.write("[POST] The data was:" + strPostdata) request = urllib2.Request(self.request.query_string) request.add_data(str(strPostdata)) response = urllib2.urlopen(request) html = self.response.out.write(unicode(html,"latin1")) def main(): application = webapp.WSGIApplication([('/', MainHandler)], debug=True) util.run_wsgi_app(application) if __name__ == '__main__': main()
Categories: Uncategorized

What IP address does Google AppEngine make requests from?

If you make a HTTP request from an AppEngine App, you will find that the IP address is different from the IP address of the URL the app is hosted on.

Here is a trick, host a proxy on your AppEngine Account, (see for steps), then make a request to via the proxy, like this:

And you get this response:

IP Information:
ISP: Google
Organization: Google
Proxy: Network Sharing Device
City: New York
Region: New York
Country: United States

This does change based on different accounts, I’ve also seen, but the 74.125 prefix seems

Categories: Uncategorized

Downcasting using Reflection C#

You cannot cast an object to a derived type in C#, this is called downcasting.


class MyBase {}
class MyDerived : MyBase {}

MyBase SomeBase = new MyBase();
MyDerived SomeDerived = (MyDerived)SomeBase;

Will fail, as will

MyDerived SomeDerived = SomeBase as MyDerived

– Which will set SomeDerived to Null.

A way around this is to use reflection:

    public MyDerived(MyBase baseClass)
        foreach (PropertyInfo piBase in typeof(MyBase).GetProperties())
            PropertyInfo piThis = GetType().GetProperty(piBase.Name);
            piThis.SetValue(this, piBase.GetValue(baseClass, null), null);
Categories: Uncategorized

Calling Python from C#

Here is a simple example of how to call an IronPython function from C#

Here is the PY file: (With URL & Regex removed)

import urllib2
import re
import array

def getRoutes():
    strRouteRegex = '(?<From>[A-Z]{3})...\d+..iata...=..(?<To>[A-Z]{3})'
    httpRequest = urllib2.urlopen("")
    html =
    routesArray = []
    for m in re.finditer(strRouteRegex, html):
        route = FlightRoute()
        route.depart ='From')
        route.arrive ='To')
    return routesArray;

class FlightRoute:
    depart = ''
    arrive = ''

And here is the C# calling code – This requires the .NET 4 to handle the dynamic types.

using System;
using System.Collections.Generic;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace CSPY
    class Program
        static void Main(string[] args)
            const string strPy = @"C:\research\";
            ScriptEngine engine = Python.CreateEngine();
            var lPath = new List<string> {@"c:\Program Files\IronPython 2.7\Lib"};
            ScriptRuntime scripting = engine.Runtime;
            dynamic runtime = scripting.UseFile(strPy);
            dynamic routes = runtime.getRoutes();
            foreach(dynamic route in routes)
                Console.WriteLine(route.depart + " -> " + route.arrive);
Categories: Uncategorized

Load Image as Base64 string

In JavaScript you can display an image like this <img src=”data:image/jpg;base64,…”> where “…” is the base64 encoded version of the image. Here is some C# Code that can convert an image on-disk to this Base64 string format

     FileInfo fi = new FileInfo(strFileName);
     FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read,FileShare.None);
     byte[] rawData = new byte[fi.Length];
     fs.Read(rawData, 0, (int)fi.Length);
     string strBase64 = Convert.ToBase64String(rawData);
     Response.Write("data:image/jpg;base64," + strBase64);
Categories: Uncategorized

PuTTY / SSH on Palm WebOS

Shown opposite is a screenshot of an application that I’ve developed for Palm / WebOS using PhoneGap that enables you connect to a Linux server via SSH (PuTTY) from your Palm Phone.

How it works, is that you have to first install a piece of software called MobilePutty that you can download from , this application must be installed and run on a Windows PC. On this program, you enter the Host IP address, username and password of your Linux Server. And a PuTTY terminal window will appear.

From your phone, you can then enter in the host name, username and password, and you will be relayed screenshots of the PuTTY terminal to your phone. You can also send commands to your PuTTY terminal, in order to interact with your linux server. You can also open up any other terminal-style program, such as windows command prompt, and remotely control your home PC from your phone, without having to worry about firewalls etc.

This application works best over WiFi broadband, be very patient if you are using GPRS.

Categories: Uncategorized