Skip to content

Instantly share code, notes, and snippets.

@rhamedy
Created June 12, 2020 03:55
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 rhamedy/97bd27ac748204cdbb9e439e4d268bdd to your computer and use it in GitHub Desktop.
Save rhamedy/97bd27ac748204cdbb9e439e4d268bdd to your computer and use it in GitHub Desktop.
Run the following code snippet in multiple by calling a somewhat slow endpoint and if you comment out line 19-23 then you will observe IOException:Broken pipe
private static void makeGetCall(URL url) throws IOException {
//make connection
HttpURLConnection myURLConnection = (HttpURLConnection) url.openConnection();
myURLConnection.setRequestProperty("Authorization", "token/whatever");
myURLConnection.setRequestMethod("GET");
myURLConnection.setRequestProperty("Content-Type", "application/json");
myURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)");
myURLConnection.setRequestProperty("Content-Language", "en-US");
myURLConnection.setUseCaches(false);
myURLConnection.setDoInput(true);
myURLConnection.setDoOutput(true);
myURLConnection.setAllowUserInteraction(false);
//get result
BufferedReader br = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
String line;
StringBuffer response = new StringBuffer();
// Uncommenting the following lines would not result in Broken pipe exception.
// if (true) {
// while ((line = br.readLine()) != null) {
// response.append(line);
// }
// }
br.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment