Added handling for HTTP errors
Fixed closing input stream