Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to send get request in java

package com.journaldev.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpURLConnectionExample {

	private static final String USER_AGENT = "Mozilla/5.0";

	private static final String GET_URL = "https://localhost:9090/SpringMVCExample";

	private static final String POST_URL = "https://localhost:9090/SpringMVCExample/home";

	private static final String POST_PARAMS = "userName=Pankaj";

	public static void main(String[] args) throws IOException {

		sendGET();
		System.out.println("GET DONE");
		sendPOST();
		System.out.println("POST DONE");
	}

	private static void sendGET() throws IOException {
		URL obj = new URL(GET_URL);
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
		con.setRequestMethod("GET");
		con.setRequestProperty("User-Agent", USER_AGENT);
		int responseCode = con.getResponseCode();
		System.out.println("GET Response Code :: " + responseCode);
		if (responseCode == HttpURLConnection.HTTP_OK) { // success
			BufferedReader in = new BufferedReader(new InputStreamReader(
					con.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();

			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();

			// print result
			System.out.println(response.toString());
		} else {
			System.out.println("GET request not worked");
		}

	}

	private static void sendPOST() throws IOException {
		URL obj = new URL(POST_URL);
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
		con.setRequestMethod("POST");
		con.setRequestProperty("User-Agent", USER_AGENT);

		// For POST only - START
		con.setDoOutput(true);
		OutputStream os = con.getOutputStream();
		os.write(POST_PARAMS.getBytes());
		os.flush();
		os.close();
		// For POST only - END

		int responseCode = con.getResponseCode();
		System.out.println("POST Response Code :: " + responseCode);

		if (responseCode == HttpURLConnection.HTTP_OK) { //success
			BufferedReader in = new BufferedReader(new InputStreamReader(
					con.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();

			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();

			// print result
			System.out.println(response.toString());
		} else {
			System.out.println("POST request not worked");
		}
	}

}
Comment

java send request

URL url = new URL("http://example.com");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
Comment

PREVIOUS NEXT
Code Example
Java :: split string 2 characters java 
Java :: regex pattern for date validation 
Java :: get average of all the array items android 
Java :: string to float java 
Java :: how to get key value from json object in java 
Java :: why bufferedreader is faster than scanner 
Java :: how to change color of progress bar in android 
Java :: java.lang.IllegalArgumentException: Invalid character found in method name 
Java :: java variable in string 
Java :: java cast int to string 
Java :: java for 
Java :: what method is use for getting the index position of a character of a string in java 
Java :: how to iterate pixels image java 
Java :: java syntax for object creation 
Java :: core java tutorial 
Java :: change string into binary uding java 
Java :: java convert java.util.Date to LocalDate 
Java :: filter arraylist java 
Java :: How to determine whether a graph contains a cycle, in Java? 
Java :: assert throws exception java 
Java :: how to print something in java 
Java :: How to efficiently shift a linked list by k positions, in Java? 
Java :: java median of list 
Java :: start activity for result deprecate 
Java :: How to build a Sudoku solver, in Java? 
Java :: arraylist to array java 
Java :: android view set border programmatically 
Java :: java android edit text set value 
Java :: replace character in string java 
Java :: BST in java 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =