Felhasználói eszközök

Eszközök a webhelyen


oktatas:programozas:java:java_rest_api_kliens:httpurlconnection

< Java REST API kliens

Java REST API kliens

Használatra példa

Client.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class Client {
  public static void connect() throws IOException {
    URL url = new URL("https://jsonplaceholder.typicode.com/users/");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.connect();
 
    InputStream is = conn.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
 
    StringBuffer sb = new StringBuffer();
    String line = br.readLine();
    while (line != null) {
      sb.append(line);
      sb.append(System.lineSeparator());
      line = br.readLine();
    }    
    conn.disconnect();
    System.out.println(sb.toString());
 
  }
}
App.java
public class App {
    public static void main(String[] args) throws Exception {
        Client.connect();
    }
}
oktatas/programozas/java/java_rest_api_kliens/httpurlconnection.txt · Utolsó módosítás: 2024/10/31 22:36 szerkesztette: admin