Connection refused normally means there was nothing listening on the port you tried to connect to, or possibly, that you tried to connect with the wrong protocol (i.e. http where https was required). Check your url and whether you should maybe be using HttpsUrlConnection rather than HttpUrlConnection.
Ananth, Your program has 2 issues: unable to connect to URL, and incorrect logic to read data into a buffer. I have modified your program and tested with localhost. Note: use of Reader to read data directly as char instead of byte