1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-06-16 16:44:53 +02:00

Add existingconnection to opoc:networkutils:performCall

This commit is contained in:
Gregor Santner 2018-04-08 20:12:51 +02:00
parent 38cf36b46c
commit 9da1a910b3
No known key found for this signature in database
GPG key ID: 7E83A7834AECB009

View file

@ -64,7 +64,9 @@ public class NetworkUtils {
connection = (HttpURLConnection) url.openConnection(); connection = (HttpURLConnection) url.openConnection();
} }
connection.connect(); connection.connect();
input = connection.getInputStream(); input = connection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST
? connection.getInputStream() : connection.getErrorStream();
if (!outFile.getParentFile().isDirectory()) if (!outFile.getParentFile().isDirectory())
if (!outFile.getParentFile().mkdirs()) if (!outFile.getParentFile().mkdirs())
@ -145,20 +147,28 @@ public class NetworkUtils {
} }
private static String performCall(final URL url, final String method, final String data) { private static String performCall(final URL url, final String method, final String data) {
return performCall(url, method, data, null);
}
private static String performCall(final URL url, final String method, final String data, final HttpURLConnection existingConnection) {
try { try {
final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); final HttpURLConnection connection = existingConnection != null
conn.setRequestMethod(method); ? existingConnection : (HttpURLConnection) url.openConnection();
conn.setDoInput(true); connection.setRequestMethod(method);
connection.setDoInput(true);
if (data != null && !data.isEmpty()) { if (data != null && !data.isEmpty()) {
conn.setDoOutput(true); connection.setDoOutput(true);
final OutputStream output = conn.getOutputStream(); final OutputStream output = connection.getOutputStream();
output.write(data.getBytes(Charset.forName(UTF8))); output.write(data.getBytes(Charset.forName(UTF8)));
output.flush(); output.flush();
output.close(); output.close();
} }
return FileUtils.readCloseTextStream(conn.getInputStream()); InputStream input = connection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST
? connection.getInputStream() : connection.getErrorStream();
return FileUtils.readCloseTextStream(connection.getInputStream());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }