Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Send HTTP Get Request with Parameters.

package com.chillyfacts.com;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL; 
import org.json.JSONObject;
public class Send_HTTP_Request2 {
	public static void main(String[] args) {
     try {
         Send_HTTP_Request2.call_me();
        } catch (Exception e) {
         e.printStackTrace();
       }
     }
	   
public static void call_me() throws Exception {
     String url = "http://api.ipinfodb.com/v3/ip-city/?key=d64fcfdfacc213c7ddf4ef911dfe97b55e4696be3532bf8302876c09ebd06b&ip=74.125.45.100&format=json";
     URL obj = new URL(url);
     HttpURLConnection con = (HttpURLConnection) obj.openConnection();
     // optional default is GET
     con.setRequestMethod("GET");
     //add request header
     con.setRequestProperty("User-Agent", "Mozilla/5.0");
     int responseCode = con.getResponseCode();
     System.out.println("
Sending 'GET' request to URL : " + url);
     System.out.println("Response Code : " + responseCode);
     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 in String
     System.out.println(response.toString());
     //Read JSON response and print
     JSONObject myResponse = new JSONObject(response.toString());
     System.out.println("result after Reading JSON Response");
     System.out.println("statusCode- "+myResponse.getString("statusCode"));
     System.out.println("statusMessage- "+myResponse.getString("statusMessage"));
     System.out.println("ipAddress- "+myResponse.getString("ipAddress"));
     System.out.println("countryCode- "+myResponse.getString("countryCode"));
     System.out.println("countryName- "+myResponse.getString("countryName"));
     System.out.println("regionName- "+myResponse.getString("regionName"));
     System.out.println("cityName- "+myResponse.getString("cityName"));
     System.out.println("zipCode- "+myResponse.getString("zipCode"));
     System.out.println("latitude- "+myResponse.getString("latitude"));
     System.out.println("longitude- "+myResponse.getString("longitude"));
     System.out.println("timeZone- "+myResponse.getString("timeZone"));  
   }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Java float Keyword 
Java :: long in java 
Java :: how to sort linked list in java 
Java :: java mahout get clusters centers 
Java :: save file to disk java 
Java :: java stream Return sums of elements grouped by a remainder of division by the give divisor 
Java :: How to send saved CSV file via email 
Java :: output of this 
Java :: madhava kripa 
Java :: using for loop to search for an input java 
Java :: Unrolling java - from 
Java :: from which android version onwards cardelevation supports? 
Java :: how to find poiwer in java 
Java :: restore 
Java :: programme javascrip mineur et majaur 
Java :: full screen in libgdx Lwjgl3 
Java :: reverse words in a string java 
Java :: java awt bild einfügen 
Java :: what is difference between constant and final in java 
Java :: unique numbers in array java 
Java :: programa que convierete un archi de c a java 
Java :: use flatpickr in javascripot 
Java :: @exceptionhandler spring boot annotation not found 
Java :: android frame to bitmap is null 
Java :: what is datasnapshot.getkey() in android studio 
Java :: jfxsa-run-no-another-jvm java fxml error 
Java :: android on change class event 
Java :: capitalize a letter in java 
Java :: action media scanner scan file android 30 deprecated 
Java :: spring-boot java header Content-Type constant 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =