How to send HTTP request GET/POST in Java

In this post, we will see how to send HTTP Get/Post in java.
There are many times when you need to send http get or post request. You can use HttpURLConnection for sending get/post request in java. It belongs to java.net package.

HttpURLConnection example:

We are going to send get or post request to URLs used in restful web services CRUD example, so instead of using PostMan , we will send request using java.

Get Url:

It will give you list of country in json format.

Post URL:

It will create a new country resource and add it to list of counties.
Java code:
When you run above program, you will get below output:
If you comment http.sendingPostRequest(); and send only get request. You will get below output:
You can see USA is added to country list.

Related Posts

  • 04 August

    How to check if session exists or not in java

    In this post, we will see how to check if HTTP session exists or not in java.  Sometimes you need to check if session already exists or not and based on that perform some operations. If you use below code to get the session: [crayon-616c6cea33924271151022/] You will never get above session object as null because […]

  • 01 August

    How To Get HTTP Response Header In Java

    In this post, we will see how to get HTTP response header in java. We have already seen how to send get or post request in java. I am using same example to demonstrate to get HTTP response header. [crayon-616c6cea342ff736972001/] [crayon-616c6cea34306587793195/] When you run above code, you will get below output: [crayon-616c6cea3430a757689655/]

  • 01 August

    How to get HTTP Request Header In Java

    In this post , we will see how to get HTTP request header in java. Sometimes, you want to print request header values. It is very simple to do it. You first need to get request object, then call getHeaderFields()  on it to get all request header values. [crayon-616c6cea34c25629748209/] Spring MVC: If you are using […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.