Z
Z
zyusifov112021-03-03 14:41:04
Java
zyusifov11, 2021-03-03 14:41:04

how to make a get request java?

URL url = new URL(urlAdress);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.disconnect();

But the request does not go through and there are no errors

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-03-03
@zyusifov11

The answer to your question is easily found by a search engine.
Here is an example:
https://www.baeldung.com/java-http-request
Here is an example code. Just handle exceptions and tweak for yourself

public class Main {


  public static void main(String[] args) {
    final String urlAdress = "http://example.com"; // url куда нужно совершить запрос
    URL url = new URL(urlAdress);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    int status = con.getResponseCode(); // совершаем запрос
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); // читаем ответ
    String inputLine;
    StringBuffer content = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        content.append(inputLine);
    }
    in.close();
    con.disconnect();
  }

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question