The HttpURLConnection
class from java.net
package can be used to send a Java HTTP Request programmatically. In this article, you will learn how to use HttpURLConnection
in a Java program to send GET
and POST
requests and then print the response.
For this HttpURLConnection
example, you should have completed the Spring MVC Tutorial because it has URLs for GET
and POST
HTTP methods.
Consider deploying to a localhost
Tomcat server.
Java HTTP GET Request
localhost:8080/SpringMVCExample/
Java HTTP GET Request for Login Page
localhost:8080/SpringMVCExample/login
Java HTTP POST Request
localhost:8080/SpringMVCExample?userName=Pankaj
localhost:8080/SpringMVCExample/login?userName=Pankaj&pwd=apple123
- for multiple paramsThe HTML of the login page contains the following form:
method
is POST
.action
is home
.
localhost:8080/SpringMVCExample/home
userName
is of type text
.You can construct a POST
request to:
localhost:8080/SpringMVCExample/home?userName=Pankaj
This will serve as the basis for the HttpURLConnection
example.
HttpURLConnection
ExampleHere are the steps for sending Java HTTP requests using HttpURLConnection
class:
URL
object from the GET
or POST
URL String.openConnection()
method on the URL object that returns an instance of HttpURLConnection
.HttpURLConnection
instance (default value is GET
).setRequestProperty()
method on HttpURLConnection
instance to set request header values (such as "User-Agent"
, "Accept-Language"
, etc).getResponseCode()
to get the response HTTP code. This way, we know if the request was processed successfully or if there was any HTTP error message thrown.GET
, use Reader
and InputStream
to read the response and process it accordingly.POST
, before the code handles the response, it needs to get the OutputStream
from the HttpURLConnection
instance and write POST
parameters into it.Here is an example program that uses HttpURLConnection
to send Java GET
and POST
requests:
Compile and run the code. It will produce the following output:
OutputGET Response Code :: 200
<html><head> <title>Home</title></head><body><h1> Hello world! </h1><P> The time on the server is March 6, 2015 9:31:04 PM IST. </P></body></html>
GET DONE
POST Response Code :: 200
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>User Home Page</title></head><body><h3>Hi Pankaj</h3></body></html>
POST DONE
Compare this output to the browser HTTP response.
If you have to send GET
and POST
requests over HTTPS protocol, then you need to use javax.net.ssl.HttpsURLConnection
instead of java.net.HttpURLConnection
. HttpsURLConnection
will handle the SSL handshake and encryption.
In this article, you learned how to use HttpURLConnection
in a Java program to send GET
and POST
requests and then print the response.
Continue your learning with more Java tutorials.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Hi, I wast tried to hit an end point which is get to fetch the detail using apache http, the json response that I get has got some attributes which are empty array. But the same end point I hit thru a browser or a rest client I don’t see these attributes as part of json response. Why is that ?
- abhineet
Thanks man! This really worked as expected. Good job (y)
- Gurgen
Nice tutorial. Well I didn’t get on thing. `os.write(POST_PARAMS.getBytes()); os.flush(); os.close();` How can I print my complete POST URL, after adding parameters. Is that possible to output the URL?
- akshay
This tutorial is great and clear. But how about 2 parameters?
- mbrean
Thanks :D
- Rinaldes Duma
hello am getting error : javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake. I did same as above except for https connection. please help.
- KIRAN
Many thanks - I’ve been puzzling over how to POST from a Spring Controller and your article was a great help. Andrew (Sheffield UK)
- Andrew
is there any way to get request url from browser console
- suman
I have to say I’m new to Java but have worked with C, C++ , python and several other languages. Java was the hardest for me to understand and work with but this little tutorial made it very plain how to use the java.net module the way I wanted. Very Nice Job!
- Mathew Eickmeyer
Really awesome tutorials so easy to understand. Thanks pankaj.
- Nidh