Created
June 22, 2021 11:01
-
-
Save kshitijvarshne1/416342da8ee5aaa9d7227f8385b7bf7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Created by IntelliJ IDEA. | |
* Author: Kshitij Varshney (kshitijvarshne1) | |
* Date: 22-Jun-21 | |
* Time: 4:22 PM | |
* File: Test.java | |
*/ | |
/* | |
import org.json.JSONArray; | |
import org.json.JSONObject;*/ | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
public class Test { | |
public static void main(String[] args) throws IOException { | |
URL url = new URL("https://jsonplaceholder.typicode.com/todos/1"); | |
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); | |
httpURLConnection.setRequestMethod("GET"); | |
httpURLConnection.setRequestProperty("Contents-Type", "application/json"); | |
httpURLConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"); | |
int n = httpURLConnection.getResponseCode(); | |
System.out.println(n); | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); | |
StringBuffer stringBuffer = new StringBuffer(); | |
String outStr=""; | |
while ((outStr = bufferedReader.readLine()) != null) { | |
stringBuffer.append(outStr); | |
} | |
System.out.println(stringBuffer.toString()); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment