Can I buy a contracted iphone from someone then unlock it?

I'm planning to buy a Iphone 4s online. It is with AT&T, so can I buy it then unlock it myself? If I could, how?

Starting last week it is now illegal to unlock a phone unless you pay full price for the phone, or you have passed your contract end date.

It's actually not legal to do that in the US now. You can buy a phone unlocked at the factory but you can't unlock one yourself.

You can definitely buy the iPhone 4S locked to AT&T network from online and unlock it.
There are various methods to unlock an iPhone. One among them is the remote unlocking service. Using this method of unlocking you can unlock your iPhone 4S safer and easier just being from your home through online. You can get the remote unlocking service for an iPhone 4S from http://www.classicunlocking.com/apple-iphone-4s/rs16wp9/ at affordable cost with free unlocking instructions. This method of unlocking doesn't require a jailbreak too. Good luck mate.
NOTE: According to the new law in USA, Unlocking Mobile phones purchased before January 26th 2013 is not illegal. Whereas unlocking mobile phones purchased January 26th 2013 is illegal. To re-legalize unlocking phones people can sign in the online petition from https://petitions.whitehouse.gov/petition/make-unlocking-cell-phones-legal/1g9KhZG7 The petition closes on 23/02/2013.

Everyone here is partially correct. As of January 26, 2013 if you purchased a phone on or after that date, it is illegal to unlock it in the US That being said, there are certain exemptions to the DMCA that allow you to be able to unlock legacy devices and devices purchased before that date with permission from your carrier.

Unlock the at&t iphone here: http://gsmunlocklabs.com/unlock-iphone/

It's true that phone unlocking is now illegal as per the new DMCA ruling "however" this only applies to phones that are purchased "after" 26th of Jan 2013. This does not apply to phones that were purchased prior to this date. So if the phone in concern was purchased from AT&T before the date I mentioned then it's legal for you to unlock it.

And yes, you can definitely unlock it using a phone unlocking service.