HttpURLConnection in the JDK has a bizarre bug. When it receives a HTTP response code in the 3xx range, it performs redirection to the web page mentioned in the “Location” header. However, when it does this, it forgets to send all the cookies with the request. Therefore, if you are working with any of the web 2.0 APIs which perform redirection and require this authentication cookie with each request, you are doomed because the server will redirect to the login page thinking that this is an unauthenticated request.

The solution for this is pretty simple. If you have written code like below:

URL u = new URL(“http://www.example.com”);

HttpURLConnection conn = (HttpURLConnection) u.openConnection();

You have to add one more line that sets redirection to “FALSE” & do the redirection yourself with the cookies, which becomes like:

URL u = new URL(“http://www.example.com”);

HttpURLConnection conn = (HttpURLConnection) u.openConnection();

conn.setInstanceFollowRedirects(false); // do not redirect

String locHeader = conn.getHeaderField(“Location”); // get the location

String cookies = conn.getHeaderField(“Set-Cookie”); // get the cookie

u = null; conn = null;

u = new URL(locHeader);

conn = (HttpURLConnection) u.openConnection();

conn.setRequestProperty(“Cookie”, cookies); // set the cookie yourself

….

Done! That should solve the bug. If this seems too much to do, then you should consider using this excellent HttpClient library from Apache.

I did the above mentioned stuff for this Free SMS Java Library because:

  1. The library has to be very small because it is just one file.
  2. Eliminate external dependencies that will bloat the library.
There you go! Now, if you have encountered this bug you have the solution to squash it. Have fun!

There are a number of use-cases for which you would want to browse a mobile-optimized website on your PC. When you visit the mobile website on your PC’s web browser, the website displays the full content much to your dismay. However, when you visit the browser on a mobile, it displays a perfectly mobile optimized page.

In these cases, there are some simple steps that you can follow to open mobile websites on your PC:

  1. Download and install the latest version Firefox from http://www.getfirefox.com/.
  2. Visit Firefox Add-ons page and download the Modify Headers addon.
  3. Install the addon and restart firefox.
  4. From the firefox window, select “Tools” menu & click on “Modify Headers” option (Tools -> Modify Headers).
  5. The window will open as shown:
    Modify Headers Addon Window Click to enlarge
  6. Below the title bar, there’s a drop down. Select “Add” from the drop down box.
  7. Now in the text box next to the drop down, type “user-agent”.
  8. In the third text box, paste this string – Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaE51-1/100.34.20; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413.
  9. Click on “Save”.
  10. The screen should look like as shown in the screen shot below:
    Filled screen of the addon Click to enlarge
  11. Using the buttons on the Modify Headers addon window, you can enable or disable particular items.
  12. That’s it! Whenever you want to view a mobile website, just go to Tools->Modify Headers and enable the user agent you added in step 8. When you don’t need it, just open this window and disable it.
  13. You can close addon window after you have enabled/disabled items.
  14. Enjoy! 🙂

Behind the Scenes:

Most mobile websites make use of the HTTP header called “User-Agent”. This header tells the server what kind of web browser the user has. So, mobile websites usually look for the name of the device manufacturer in the User-Agent header. If it matches one of the manufacturers then the mobile optimized web page is sent.

That’s about it! Hope you enjoyed this brief.

Now go and figure out how to display the iPhone version of Facebook on your PC 😉

Everyone of you would have surely heard of Automatic Teller Machines & would have used one at least once. I use ATMs almost 2-3 times a week & find it frustrating that I have to navigate through a whole lot of screens to withdraw a small amount of cash. This blog post is an attempt to suggest a new user interface to ‘get things done’ faster & leave way for others.

Existing Screens

When you enter an ATM, you pop in your card in the card slot and then go through the following screens (or minor variations of them):

  1. Select language of choice.
  2. Enter your PIN.
  3. Next screen contains a slew of options such as Balance Enquiry, Mini Statement, PIN change, Cash Withdrawal, Fast Cash, Cheque Book Request etc.
  4. You select “Cash Withdrawal”
  5. You have to select whether its Savings Account or Current Account.
  6. Enter the amount in multiples of 100.
  7. Answer whether you require a printed receipt
  8. Wait for cash
  9. Answer whether you wish to do another transaction (“Yes” takes you to step 2).
Most people I know go to ATM to do one of the following 3 things:
  1. Withdraw Cash
  2. Balance Enquiry
  3. Mini Statement

New Screen Proposal

As soon as you enter your PIN in step 2, you should be directly taken to step 6. Since there are 4 buttons to the left and right of the screen, they could be used to provide other options. The four buttons on the left could contain Balance Enquiry, Mini Statement, Fast Cash & More…

This has lot of benefits:

  1. The amount of time being spent in the ATM to withdraw cash comes down drastically. To perform the 9 steps above, it takes approximately 3 minutes (counted yesterday). For aged people & new comers, it would take even more. So, during peak hours, if a huge crowd is waiting outside, imagine the time it would take for the last guy in the queue to get cash quickly. Now, jumping from step 2 to 6 in two screens will take less than a minute and things can happen quickly.
  2. Most new comers are intimidated at the number of options that are being shown. The data stored in the bank’s servers could be used to eliminate screen 5.
  3. A new option containing the language can also be stored in the bank’s backend so that screen 1 can be removed for further transactions from the same ATM card.
  4. Similarly, in step 6, since the ATM accepts only amount entered in multiples of 100, it needn’t display the last “.00”. For e.g. if you need to take Rs. 500, you have to enter ‘5’ followed by four zeros. I have personally helped an old person who couldn’t understand why the ATM wouldn’t dispense cash even though he entered ‘3’ ‘0’ ‘0’. On the screen, it showed as “3.00” and he couldn’t see the ‘.’ clearly. That is pretty bad from the usability point of view.
  5. Further, ATMs can have touch screens (State Bank of India has this). Touch screens are more usable than pressing mundane buttons on the screen. 
Do you agree with the points above? Please let me know if this can be suggested to banks. 

Recently I needed a way for Threaded Tweets to do HTTP basic authentication to try out some stuff. As you probably know by now, it is written in javascript and uses no server side code. So, I had to find out some way to pass basic authentication credentials through the XMLHttpRequest object so that I could be authenticated with the Twitter server.

Turns out that it is pretty simple. If you use FireBug on any website that supports Basic Authentication, you will note that a new HTTP header called ‘Authorization’ is added. It looks like:

Authorization: Basic aDkdjfZy==

Now all you need to do is somehow pass on this header to the XMLHttpRequest object. If you use the native XMLHttpRequest, you can use the setRequestHeader method to do this:

xhr.setRequestHeader(“Authorization”, “Basic aDkdjfZy”);

What if you are using jQuery? Then also its pretty simple. jQuery’s AJAX object allows us to change the XHR object before the AJAX request is sent by setting the “beforeSend” callback; which is done as follows:

$.ajax ( {
url: “http://abc.com/”,
beforeSend: function (xhr) { xhr.setRequestHeader (“Authorization”, “Basic aDkdjfZy==”); },
succes: function(val) { alert(val); } } );

Base 64 Encoding:

One important part of Basic Authentication is that, you need to encode the username and password into Base 64. There are tons of Base 64 encoding tools written on javascript, which you can use. Example below:

xhr.setRequestHeader(“Authorization”, “Basic ” + encodeBase64 (“username:password”) );

That’s about it. Hope this was useful to you !

Some Updates

Cross-domain GET requests work all the time. The problem is with POST requests. Most browsers don’t allow XHR to POST data to a domain that’s different from the one in which the page is loaded. For this to work correctly, the ‘document.domain’ property must be set correctly.

The second best way to POST to another domain is to use an iframe and submit the form with the iframe as the “target”. Again, to read the result of the iframe, the “document.domain” of the parent and that of the iframe should match. This can be easily set through javascript. Thus, when the form post is done and the page is loaded, its value can be accessed.

Note: Chrome and Safari don’t allow access to iframe’s DOM if its document.domain is different from that of the iframe’s parent.

That is a very common error that users generally encounter when packages are being compiled. Though the log files mention something bizarre, this error can be resolved quite easily by running the following command:

$ sudo apt-get install build-essential

That would do for most cases. Many programs also require a compiler-compiler called ‘bison’. You may install that also by giving,

$ sudo apt-get install bison

This would make sure “configure” scripts work properly.