Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7)
In my experience it's a lot easier on a smaller plane (ie domestic/Europe). I've been twice, once at LHR and the other at LGW - both after the flight.
The best approach is to ask the flight deck on the way out, usually one of the pilots is visible and just a simple "excuse me" is enough to grab their attention. One time we asked an FA, but they seemed a bit reluctant, luckily this was in ear shot of a pilot so they promised us a little tour.
Both times the pilots were extremely friendly and seemed to have all the time for us (around 20 minutes once). It's much better to ask on domestics because the pilots said they get bored flying back and forth and have a bit of a wait till the next flight. Any other short flights should be fine.
But if expecting to see the flight deck on a longhaul then I'd do as Genius1 suggests and try visit before the flight - of course it particularly helps to be travelling in the closest cabin to the cockpit, ie First or Club. If in any other cabin you may not even see the pilots, so in that case find a friendly FA and just emphasise you'd like a quick photo - just incase the pilots think they'd be too tired on landing.
Also I may be wrong - but both times I did this (Mrs M is a bit of a nervous traveller so these visits helped enormously) the cabin crew seemed like they were waiting around, do they have to wait until all passengers have departed before they can leave? If that's true then please be considerate to them also!