Archive

Archive for July, 2011

Find All Websites on a Webserver in C#

July 31, 2011 Leave a comment

This is a handy script that can tell you how many sites are hosted on an IP address.  It uses  the Bing API, so it basically asks bing if it knows of sites on that IP address. This means that they have to be listed in bing, i.e. with public access.

 

 

 public static int SitesOnIP(string ip)
        {
            string url = "http://api.search.live.net/json.aspx?";
            url += "Appid=92B665B5421E197DC762503859279DFEBBE0B998";
            url += "&query=IP:" + ip;
            url += "&sources=web&web.count=50";
            // "Web":{"Total":31900
            string strRegex = @"Web....Total..(?<Count>\d+)";
            WebClient web = new WebClient();
            string strJson = web.DownloadString(url);
            string strCount = Regex.Match(strJson, strRegex).Groups["Count"].Value;
            return Convert.ToInt32(strCount);
        }
Categories: Uncategorized

A better XML to JSON proxy

July 19, 2011 Leave a comment

I was looking for a generic way to convert XML to JSON, and I came across this GAE version http://jsonproxy.appspot.com/proxy, however, I noted that it only converted the first node of any tree to JSON, so it missed out most of the data contained in the XML.

Lets say,  we wanted to load exchange rates into a mobile app running Javascript (read phonegap /Wrtkit /Webos), an XML feed can be found at http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml (European central bank), but it’s easier to work with JSON than XML in Javascript. If you use the GAE (google app engine) version, then you only get USD, so I wanted to write my own proxy to fix this.

So, with the help of a free .NET Hosting account from brinkster Which worked an absolute charm – I didn’t have to put load on my own server to run this!. I installed my own XML-To-JSON Proxy:

I’ve fixed this, in a C# Implementation:

http://<url>/default.aspx?url=http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

vs

http://jsonproxy.appspot.com/proxy?url=http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

(obviously, you need to urlencode any querystring parameters after the URL in both cases)

Categories: Uncategorized

Using JSONP to show books from the Google API.

July 6, 2011 Leave a comment

JSONP is one of the few technologies that allows cross-site scripting (XSS), whether this is by design or an oversight, here is an example of a Google Book Search using JSONP

<html>
  <head>
    <title>Books API Example</title>
  </head>
  <body>
    <div id="content"></div>
    <script>
      function handleResponse(response) {
	  html = "";
      for (var i = 0; i < response.items.length; i++) {
        var item = response.items[i];
        // in production code, item.text should have the HTML entities escaped.
		html += "<hr><img src=" + item.volumeInfo.imageLinks.thumbnail + ">";
		html += "<br>" + item.volumeInfo.title;
		html += "<br>Written by ";
		for(var author in item.volumeInfo.authors)
		{
			html+= item.volumeInfo.authors[author] + " ";
		}
		html += "<br>Published by " + item.volumeInfo.publisher;
		html += "<br>Published on " + item.volumeInfo.publishedDate;
		if (item.volumeInfo.pageCount != undefined)
		{
			html += "<br>Pages " + item.volumeInfo.pageCount;
		}
		for (var identifier in item.volumeInfo.industryIdentifiers)
		{
			var isbn = item.volumeInfo.industryIdentifiers[identifier];
			if (isbn.type=="ISBN_10")
			{
				html += "<br><a href=http://www.amazon.com/exec/obidos/ASIN/" + isbn.identifier+ "/httpnetwoprog-20>";
				html += "Buy at Amazon USA</a>";
				html += "<br><a href=http://www.amazon.co.uk/exec/obidos/ASIN/" + isbn.identifier+ "/wwwxamlnet-21>";
				html += "Buy at Amazon UK</a>";
			}
		}
	  }
	  document.getElementById("content").innerHTML = html;
    }
    </script>
    <script src="https://www.googleapis.com/books/v1/volumes?q=harry+potter&callback=handleResponse"></script>
  </body>
</html>

 

Categories: Uncategorized