try {
cnx = (HttpURLConnection) openProxiedConnection(url);
// If we get a null connection, then throw an exception
if (cnx == null) {
throw new MovieDbException(MovieDbException.MovieDbExceptionType.CONNECTION_ERROR, "No HTTP connection could be made.", url);
}
if (isDeleteRequest) {
cnx.setDoOutput(true);
cnx.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
cnx.setRequestMethod("DELETE");
}
sendHeader(cnx);
if (StringUtils.isNotBlank(jsonBody)) {
cnx.setDoOutput(true);
wr = new OutputStreamWriter(cnx.getOutputStream());
wr.write(jsonBody);
}
readHeader(cnx);
// http://stackoverflow.com/questions/4633048/httpurlconnection-reading-response-content-on-403-error
if (cnx.getResponseCode() >= 400) {
in = new BufferedReader(new InputStreamReader(cnx.getErrorStream(), getCharset(cnx)));
} else {
in = new BufferedReader(new InputStreamReader(cnx.getInputStream(), getCharset(cnx)));
}
String line;
while ((line = in.readLine()) != null) {
content.write(line);
}
} finally {
if (wr != null) {
wr.flush();
wr.close();
}
if (in != null) {
in.close();
}
if (cnx instanceof HttpURLConnection) {
((HttpURLConnection) cnx).disconnect();
}
}
return content.toString();
} catch (IOException ex) {
throw new MovieDbException(MovieDbException.MovieDbExceptionType.CONNECTION_ERROR, null, url, ex);
} finally {
if (content != null) {
try {
content.close();
} catch (IOException ex) {