private Long doAuthenticatedPost(String url, String body) throws Exception {
HttpPost request = new HttpPost(url + "?oauth_token=" + oauthToken);
HttpResponse response = null;
try {
HttpEntity entity = new StringEntity(body, HTTP.UTF_8);
// set the content type to json
request.setHeader("Content-Type", "application/json; charset=utf-8");
request.setEntity(entity);
// sign the request
// send the request
response = httpClient.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 401) {
throw new RuntimeException("unable to execute POST - url: "
+ url
+ " body: "
+ body);
}
if (statusCode == 201) {
Header loc = response.getFirstHeader("Location");
if (loc != null) {
String locS = loc.getValue();
if (!StringUtils.isBlank(locS) && locS.matches(".*/[0-9]+$")) {
try {
return NumberUtils.createLong(
locS.substring(locS.lastIndexOf("/") + 1));
} catch (NumberFormatException e) {
return null;
}
}
}
}
return null;
} finally {
try {
if (response != null) {
HttpEntity entity = response.getEntity();
if (entity != null) {
entity.consumeContent();
}
}
//EntityUtils.consume(response.getEntity());
} catch (IOException e) {
// ignore