/
Vitalii01
/
Nasa
Обзор
Документация
Войти
/
Vitalii01
/
Nasa
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Main.java
50 строк
2 KB
Vitalii01
upload files
18 апр 2025, 15:25
18 апр 2025, 15:25
0314d13
Код
Авторство
О чём код?
import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.hc.client5.http.classic.methods.HttpGet; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.client5.http.impl.classic.HttpClients; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLParameters; import java.io.FileOutputStream; import java.io.IOException; import java.net.Authenticator; import java.net.CookieHandler; import java.net.ProxySelector; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Duration; import java.util.Arrays; import java.util.Optional; import java.util.Scanner; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; public class Main { public static void main(String[]args)throws IOException{ String url = "https://api.nasa.gov/planetary/apod?api_key=CBHuAdhatL0YiwReBDgZPl5FP71aC5DC6W4Iaalq"; InfoClass.Topic("Библеотека HttpClient","="); InfoClass.Info("Создание объектов класса HttpClient"); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); // Запрос для получение информации с сервера CloseableHttpResponse response = httpClient.execute(httpGet); // Scanner scanner = new Scanner(response.getEntity().getContent()); // while (scanner.hasNext()){ // System.out.println(scanner.nextLine()); // } ObjectMapper mapper = new ObjectMapper(); NasaAnswer answer = mapper.readValue(response.getEntity().getContent(),NasaAnswer.class); System.out.println(answer.title); System.out.println(answer.url); System.out.println(answer.date); System.out.println(answer.hdurl); System.out.println(answer.explanation); String imageUrl = answer.url; HttpGet imageLoad = new HttpGet(imageUrl); // Создаю новый запрос CloseableHttpResponse image = httpClient.execute(imageLoad); FileOutputStream fos = new FileOutputStream("images/image.jpg"); image.getEntity().writeTo(fos); } }