1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-11-22 04:12:08 +01: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.connect();
input = connection.getInputStream();
input = connection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST
? connection.getInputStream() : connection.getErrorStream();
if (!outFile.getParentFile().isDirectory())
if (!outFile.getParentFile().mkdirs())
@ -145,20 +147,28 @@ public class NetworkUtils {
}
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 {
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(method);
conn.setDoInput(true);
final HttpURLConnection connection = existingConnection != null
? existingConnection : (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);
connection.setDoInput(true);
if (data != null && !data.isEmpty()) {
conn.setDoOutput(true);
final OutputStream output = conn.getOutputStream();
connection.setDoOutput(true);
final OutputStream output = connection.getOutputStream();
output.write(data.getBytes(Charset.forName(UTF8)));
output.flush();
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) {
e.printStackTrace();
}