HTTP: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: fixed libheader)
(Updated the Java example to use the modern standard HTP Client API)
Line 1,277: Line 1,277:


=={{header|Java}}==
=={{header|Java}}==
Using the standard [http://openjdk.java.net/groups/net/httpclient/ Java 11 HTTP Client]
<lang java5>import java.util.Scanner;

import java.net.URL;
<lang java>import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.Charset;


public class Main {
public class Main {
public static void main(String[] args) throws Exception {
public static void main(String[] args) {
Scanner sc = new Scanner(new URL("http://www.rosettacode.org").openStream());
var request = HttpRequest.newBuilder(URI.create("https://www.rosettacode.org"))
while (sc.hasNext())
.GET()
System.out.println(sc.nextLine());
.build();

HttpClient.newHttpClient()
.sendAsync(request, HttpResponse.BodyHandlers.ofString(Charset.defaultCharset()))
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
}
}
}
}