Skip to content

Instantly share code, notes, and snippets.

@uphy
Created October 10, 2018 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uphy/373ebbcf5721ebb54eaabb27de9261e9 to your computer and use it in GitHub Desktop.
Save uphy/373ebbcf5721ebb54eaabb27de9261e9 to your computer and use it in GitHub Desktop.
Artifically create connection timeout error
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
URL url = new URL("http://10.255.255.1:8080");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(1000);
try {
con.connect();
} catch (SocketTimeoutException ex) {
System.out.println("Connection timeout occured");
} finally {
con.disconnect();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment