Last active
June 8, 2019 13:42
-
-
Save RightHandedMonkey/4b8e2f8893e3b20116ad18fb524a5cb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.List; | |
import java.util.Map; | |
public class NetworkTest { | |
//Run as: java -Djava.net.preferIPv6Addresses=true NetworkTest | |
public static void main(String[] args) { | |
Long nanoTime = System.nanoTime(); | |
// String url = "https://www.google.com/"; | |
String url = "https://xtvapi.cloudtv.comcast.net/"; | |
System.out.println("java.net.preferIPv6Addresses = "+System.getProperty("java.net.preferIPv6Addresses")); | |
System.out.println("java.net.preferIPv4Stack = "+System.getProperty("java.net.preferIPv4Stack")); | |
try { | |
System.out.println("Connecting to: "+url); | |
HttpURLConnection urlc = (HttpURLConnection) new URL(url).openConnection(); | |
readStream(urlc.getInputStream()); | |
String result = "Completed. Status code: " + urlc.getResponseCode() + "\n"; | |
System.out.println(result); | |
Map<String, List<String>> map = urlc.getHeaderFields(); | |
map.forEach((s, strings) -> System.out.println(s + "=" + strings)); | |
urlc.disconnect(); | |
printTime(nanoTime); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.out.println("ERROR: " + e.getMessage() + "\n"); | |
printTime(nanoTime); | |
} | |
} | |
private static void printTime(Long nanoTime) { | |
nanoTime = (System.nanoTime() - nanoTime) / 1000000; | |
System.out.println("\nElapsed time: "+nanoTime+" ms"); | |
} | |
private static String readStream(InputStream in) throws IOException { | |
String str = ""; | |
BufferedReader br = new BufferedReader(new InputStreamReader(in)); | |
String line = br.readLine(); | |
while (line != null) { | |
str += line + "\n"; | |
line = br.readLine(); | |
} | |
return str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment