-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSend_HTTP_Request2.java
More file actions
55 lines (53 loc) · 1.8 KB
/
Send_HTTP_Request2.java
File metadata and controls
55 lines (53 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.test.com;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.simple.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 IOException {
String url = "http://api.ipinfodb.com/v3/ip-city/?key=235e4582ef0437ba419e6f8c1ff70f0fac7d694d6cc4ec9095d94262e4f1064f&ip=122.170.50.209&format=json";
try{
//to handle NoRouteToHostException
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();
}
catch(Exception ex){
e.printStackTrace();
}
System.out.println("\nSending '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());
try {
//To handle FileNotFoundException
//putting the values in a json file
FileWriter file = new FileWriter("E:/output.json");
file.write(response.toString());
file.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}