Image Image Image Image Image Image Image Image Image Image

Mudassir Shahzad | December 15, 2017

Scroll to top

Top

3 Comments

How To Send HTTP Request GET/POST In Java

How To Send HTTP Request GET/POST In Java
Mudassir Shahzad

A brief Introduction

 

Client server architecture requires two parts as main resources:

  1. Client and
  2. Server

In this client server architecture, A client “requests” or “asks” the server and the server “serves” or “answers” the request.

For example, I enter “www.google.com” in my browser and press “enter”. What is happening is that I am requesting the google server to “serve” me by showing me the search page.

In this case, I am the client and GOOGLE is the server which gives me the google search page having that search bar thingy :)

Now, a request can be of two types:

  1. GET and
  2. POST

The difference between both is beyond the scope of this article, and as you can see, despite trying, my “Brief Introduction” is already getting longer, isn’t it ?

In this article, we will show you two examples to make HTTP GET/POST request via following APIs:

  1. Standard HttpURLConnection
  2. Apache HttpClient library (Discussed here)

 

Java HttpURLConnection example

 

This example uses HttpURLConnection (http) and HttpsURLConnection (https) to

  1. Send an HTTP GET request to Google.com to get the search result.
  2. Send an HTTP POST request to Apple.com search form to check the product detail.

HttpURLConnectionExample.java

Output

Sending ‘GET’ request to URL : http://www.google.com/search?q=mkyong
Response Code : 200
Google search result…

Testing 2 – Send Http POST request

Sending ‘POST’ request to URL : https://selfsolve.a`pple.com/wcResults.do
Post parameters : sn=C02G8416DRJM&cn=&locale=&caller=&num=12345
Response Code : 200
Apple product detail…

An example using Apache HttpClient library is discussed here

 

 

References

  1. MkYong article

Related posts:

Comments

  1. ray ban sunglasses sale

    How To Send HTTP Request GET/POST In Java | Mudassir Shahzad

  2. cheap michael kors handbags

    How To Send HTTP Request GET/POST In Java | Mudassir Shahzad

  3. louis vuitton outlet store

    How To Send HTTP Request GET/POST In Java | Mudassir Shahzad

Submit a Comment

Cancel reply