Monday, August 15, 2011

Writing your own twitter share button

This is a simple exercise thanks to twitter web intents. All you need is a browser that supports javascript.

The basic url looks like

https://twitter.com/intent/tweet?text=<your_text>&url=<url_of_page>

The rest is upto javascript. For I simply assign the title of the page, and for the I simply assign window.location.href. Of course I have to properly escape both the parameters into the url. Feed the complete url to a call to window.open(). Wrap all of this in an anonymous function and execute it immediately - in the browser's address bar!

You can save this script as a bookmark, and access it anytime. In fact, you can drag the below link to your favorites bar (on IE) or bookmarks bar on (FF or Google Chrome).

Share page

There are a host of other parameters for other various ways in which you can tweet, but I am stopping short here. You can explore the documentation for learn more.

Ps: These things won't work inside an IFRAME

Monday, July 4, 2011

Getting headers from a web service

When we add a web service to a project Visual Studio automatically generates proxy classes for us. This was something very unfortunate for me. It doesn't give me header information. So I can't know any header information sent from the server to the client. I had the idea of attaching machine name (of the web server) in the response, and reading this from the client (which is my asp.net application). This would have been a simple task if the web service was exposed as a script service, and, that service was being called by AJAX call.

The problem at hand

The proxy classes generated derive from the SoapHttpClientProtocol class. The derived (web service) class has a lot of functionality built into it which saves us the headache of communicating with XML web services. But, the web service class doesn't expose any way to obtain the header information.

Solution

Fortunately you can derive the web service class and obtain a reference to the WebResponse object which encapsulates the response of the asmx web method which was executed over the internet. All you have to do is inherit the web service class, override the GetWebResponse method, and, make use of this in code.
Imports WebSvcHeaders.TimeService
Imports System.Net
Public Class TimeSvcProxy
    Inherits SimpleWebService

    Private _WebResponse As WebResponse

    Protected Overrides Function GetWebResponse(ByVal request As System.Net.WebRequest) As System.Net.WebResponse
        _WebResponse = MyBase.GetWebResponse(request)
        Return _WebResponse
    End Function

    Function GetResponseHeader(ByVal HeaderKey As String) As String
        Return _WebResponse.Headers(HeaderKey)
    End Function

End Class

Why is this useful?

You can tag diagnostic information in the response headers and then read that information from the client and log it on the client itself. It can be of major help while trying to analyze performance problem with asp.net websites deployed in a web farm, for e.g. We don't know which server in a web farm might service the web method call. Hence tagging, for e.g., the machines name in the response headers and being able to read that from the client gives me a lead to do investigation on the server which processed the call to the web method.

I suppose you can do something similar with WCF web services too. But I don't know if it can be done exactly on the same lines like I've mentioned in my post.

Tuesday, April 26, 2011

Hosting from home: Part 2

This post seems to be making a comeback after a long time. It is a followup of this post, where I experimented with hosting my webserver over the internet. Here I am going to try to explain the things I did so I can serve a page off my computer, but access that page from my a laptop running windows vista. This exercise sort of kind of gave me an idea how web hosting providers work. I hosted a page of my apache server. Apache was the web server of my choice. Very complicated to do stuff in it. But you have to take care of a few things:
  1. You have to provide a server name configuration to your website:
    Till date I don't understand this feature, but all I know is if requests come in with the host address saying this, then apache will not what website it belongs to and will process
  2. Apache should be listening to a particular port:
    Usually any web server by default will listen to 8080. This is where host http traffic will enter/exit the system. If you are using anything other than 8080 you'd have to specifically mention it in the url via the browser
  3. Make note of the IP address:
    Your router will assign an IP address unique to your machine. It is this IP address you are going to make public
That said lets go to the windows machine. All you have to do is add an entry into your hosts file (%system32%\drivers\etc\hosts). It has to be in the form: [web site domain name] [IP address] E.g. deostroll.media.com 192.162.2.4 Once this is done and the config is saved. You can startup internet explorer type in the website page giving the domain name you've entered above, and you are good. I've leave you to figure how websites over the internet now work.

Sunday, October 17, 2010

The winner? Really?

Kind of read a tweet and even RTed it too. Something about which language is about to go hype next.

The author of the article neatly jots down so many languages, some of them I've never really heard of. But if you ask me. There is no one language that will make it. Or, if I were to rephrase it, almost all languages make it. This article kind of lead me into thinking how do we gauge the popularity of a language?

My views now prompts a simple answer: community adoption. And, I don't mean the community of people 'use a language' and 'make it popular'. It kind of goes on the lines of where the trend is going; what consumers are interested in. If it is more into mobile space, for e.g. say that consumers want to run heavy applications on their hand held devices, developers would look more into how to efficiently utilize the power of the processors, etc etc. Hence they'd look for a language or a product which would give them more productivity and the capability of writing complex software.

So these are just my views for the moment. Of course, other people definitely have other ideas. Please feel free to voice them.

Wednesday, August 4, 2010

MVC joke

So Model, View and Controller walk into a bar to have a drink. All the three get really high. Then the Controller slaps Model, in the face and leaves. View has her hands to her head.

What did Model say?

.
.
.
.
.
.
.
.
.
.
.
.
….

“I love View”

Wednesday, July 14, 2010

python: using simplejson to JSON serialize

When you search the web for how to json serialize you data you first encounter a module called simplejson. But using simplejson is the problem...no one exactly says how you can use it...

The simplejson library is fortunately friendly with python dictionaries. And hence here is a small example to start off with:
import simplejson

vals={}
vals['user'] = 'deostroll'
vals['visits'] = 46
vals['date_of_birth'] = '10/27/1986'

print simplejson.dumps(vals)
This library sadly doesn't know how to serialize a class object. However the work around to that is quite simple too.
class a():
    pass
    
x = a()

x.name = 'Arun'
x.designation = 'Software Engineer'
x.department = 'Media & Entertainment'
x.age = 24

print simplejson.dumps(x.__dict__)
Hope you've understood how you can now use simplejson library. Happy programming

Thursday, June 24, 2010

Hosting from home

I was recently playing around with apache and my belkin wireless router. I got the hint that I can use my computer at home to host websites over the internet from hanging around in the apache IRC channel and interacting with people there. But I didn’t know what my router was actually capable of.

Stuff you need

You need is a web server. Any web server; it needn’t be apache – what I used.

You will need a broadband connection. The thing to take care here is that your connection should have only one static IP address. It means each time you connect to the internet it should be through this IP address only.

You technically don’t need a router! But having a router is advantageous. Most modern routers have a feature called port-forwarding. If a router is configured for port-forwarding port 8083 to port 80, it will literally “forward” those packets with port address 8083 to port 80, which is the default port most web server programs bind to.

A word about the web server

I did most of my experiments on apache. Once if apache is neatly installed and running on the machine we simply open a browser and type http://localhost , to see if it works. You’d mostly get a default message like “it works” or sometimes you might even get something more colorful. It depends on what is set as the default message for the installation. I am not sure about how all the other web servers work. But there is usually some default message.

Finding your IP

This is the easiest part of this whole exercise. Just browse to www.whatismyip.com.

Or you can call your service provider and ask them to find it for you.

That’s it!!!

Instead of typing localhost in your web browser, type the ip address: i.e. http://[your_ip_address]/

You should see the default message here. If you check the server’s log you should find an entry pertaining to what you’ve just typed in your browser.

For those who have configured port-forwarding on your router, simply type http://[your_ip_address]:[port_number]/ in the browser.

Final words

It is actually very simple to use your home computer as a web host. But it is not recommended. There are many reasons for this. Foremost your machine does not have the hardware that most web hosting providers have. Your machine does not have the level of virus protection professional hosting servers have. When I was excited that I was able to make my web server serve something via the internet to some other computer elsewhere, I posted it on twitter. I left it online about for 10 min to do various sorts of testing. I had close to 10-15 hits mostly from search engine crawlers and the like.

Next steps

The next post I am going to explain how you can host your own intranet website with just your router, and also how you can give it a more lovable name instead of an IP address.