replicate timeout exception + java code example
Example: replicate timeout exception + java
package crunchify.com.tutorial;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class CrunchifyGenerateTimeout {
public static void main(String[] args) throws Exception {
new CrunchifyGenerateTimeout();
}
public CrunchifyGenerateTimeout() {
try {
String myUrl = "https://google.com/";
String results = crunchifyCallURL(myUrl);
System.out.println(results);
} catch (Exception e) {
e.printStackTrace();
}
}
private String crunchifyCallURL(String crunchifyURL) throws Exception {
URL crunchURL = null;
BufferedReader crunchReader = null;
StringBuilder crunchBuilder;
try {
crunchURL = new URL(crunchifyURL);
HttpURLConnection connection = (HttpURLConnection) crunchURL.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(10);
connection.connect();
crunchReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
crunchBuilder = new StringBuilder();
String eachLine = null;
while ((eachLine = crunchReader.readLine()) != null) {
crunchBuilder.append(eachLine + "\n");
}
return crunchBuilder.toString();
} catch (Exception et) {
et.printStackTrace();
throw et;
} finally {
if (crunchReader != null) {
try {
crunchReader.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
}
}