mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
Allow for returning of error messages (#955)
* Allow for returning of error messages * Fix request not sending before error check
This commit is contained in:
parent
af0182d116
commit
64d5390800
1 changed files with 22 additions and 14 deletions
|
@ -51,19 +51,7 @@ public class WebUtils {
|
||||||
con.setRequestMethod("GET");
|
con.setRequestMethod("GET");
|
||||||
con.setRequestProperty("User-Agent", "Geyser-" + GeyserConnector.getInstance().getPlatformType().toString() + "/" + GeyserConnector.VERSION); // Otherwise Java 8 fails on checking updates
|
con.setRequestProperty("User-Agent", "Geyser-" + GeyserConnector.getInstance().getPlatformType().toString() + "/" + GeyserConnector.VERSION); // Otherwise Java 8 fails on checking updates
|
||||||
|
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
return connectionToString(con);
|
||||||
String inputLine;
|
|
||||||
StringBuffer content = new StringBuffer();
|
|
||||||
|
|
||||||
while ((inputLine = in.readLine()) != null) {
|
|
||||||
content.append(inputLine);
|
|
||||||
content.append("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
in.close();
|
|
||||||
con.disconnect();
|
|
||||||
|
|
||||||
return content.toString();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return e.getMessage();
|
return e.getMessage();
|
||||||
}
|
}
|
||||||
|
@ -99,7 +87,27 @@ public class WebUtils {
|
||||||
out.write(postContent.getBytes(StandardCharsets.UTF_8));
|
out.write(postContent.getBytes(StandardCharsets.UTF_8));
|
||||||
out.close();
|
out.close();
|
||||||
|
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
return connectionToString(con);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the string output from the passed {@link HttpURLConnection}
|
||||||
|
*
|
||||||
|
* @param con The connection to get the string from
|
||||||
|
* @return The body of the returned page
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private static String connectionToString(HttpURLConnection con) throws IOException {
|
||||||
|
// Send the request (we dont use this but its required for getErrorStream() to work)
|
||||||
|
int code = con.getResponseCode();
|
||||||
|
|
||||||
|
// Read the error message if there is one if not just read normally
|
||||||
|
InputStream inputStream = con.getErrorStream();
|
||||||
|
if (inputStream == null) {
|
||||||
|
inputStream = con.getInputStream();
|
||||||
|
}
|
||||||
|
|
||||||
|
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
String inputLine;
|
String inputLine;
|
||||||
StringBuffer content = new StringBuffer();
|
StringBuffer content = new StringBuffer();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue