mirror of
https://github.com/gsantner/dandelion
synced 2024-11-21 20:02:07 +01:00
Add existingconnection to opoc:networkutils:performCall
This commit is contained in:
parent
38cf36b46c
commit
9da1a910b3
1 changed files with 17 additions and 7 deletions
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue