I am trying to write simple class where I try to hit google.com, but I get java.net.SocketException: Broken pipe error. I have tried to run the code below on different Java versions: 8, 14, 15, but unfortunately I get the same error irrespective of Java version.
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.HttpsURLConnection; import java.io.*; import java.net.*; import java.sql.SQLException; import java.security.KeyStore; public class Test{ /** * Utility method for getting object from url */ public static void main(String[] args){ String ret = "success"; try { String urlString; //Oh my god this is stupid! //https://stackoverflow.com/a/25735202 URL url = new URL("https://www.google.com"); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); urlString=uri.toASCIIString(); url = new URL(urlString); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setConnectTimeout(60000); con.setReadTimeout(60000); InputStream inputStream = con.getInputStream(); inputStream.close(); con.disconnect(); } catch (InvalidClassException e) { ret = "Invalid class: "+e.getMessage(); } catch (MalformedURLException e) { ret = "Malformed URL "+e.getMessage(); } catch (SocketTimeoutException e) { ret = "Timeout "+e.getMessage(); } catch (UnknownHostException e) { } catch (IOException e) { ret = "Error in reading from (possible timeout)"; } catch (URISyntaxException e) { ret = "URI syntax error "+e.getMessage(); } catch (Exception e) { ret = "Getting data from URL "+e.getMessage(); } System.out.println(ret); } }