HTTP: Difference between revisions

715 bytes added ,  1 year ago
m (Corrected command in comment.)
Line 1,325:
 
=={{header|Java}}==
The ''URL'' class offers an ''openStream'' method which will make a connection and return an ''InputStream''.
<syntaxhighlight lang="java">
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
</syntaxhighlight>
<syntaxhighlight lang="java">
void printContent(String address) throws URISyntaxException, IOException {
URL url = new URI(address).toURL();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
String line;
while ((line = reader.readLine()) != null)
System.out.println(line);
}
}
</syntaxhighlight>
<br />
Using the standard [http://openjdk.java.net/groups/net/httpclient/ Java 11 HTTP Client]
 
118

edits