Skip to content

Instantly share code, notes, and snippets.

@bbody
Created March 2, 2013 14:32
Show Gist options
  • Save bbody/5071247 to your computer and use it in GitHub Desktop.
Save bbody/5071247 to your computer and use it in GitHub Desktop.
private boolean postIssue(String description, String title, String username,
String password, String stub, String ownerUsername){
// Set up authentication
String authentication = username + ":" + password;
String encoding = Base64.encodeToString(authentication.getBytes(),
Base64.NO_WRAP);
URL url;
try {
// Setup URL with specified username (Owner) and repository stub
url = new URL("https://api.bitbucket.org/1.0/repositories/" +
ownerUsername + "/" + stub + "/issues/");
// Setup connection with specified attributes
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Basic " + encoding);
conn.setDoInput(true);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
// Setup information to send, in this example title and description
String commands = "title=" + URLEncoder.encode(title, "UTF-8")
+ "&content=" + URLEncoder.encode(description, "UTF-8");
// Set properties
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length",
Integer.toString(commands.getBytes().length));
conn.setRequestProperty("Content-Language", "en-us");
// Write data
DataOutputStream writer =
new DataOutputStream(conn.getOutputStream());
writer.writeBytes(commands);
writer.flush();
writer.close();
conn.connect();
// If control reaches here, means there were no caught exceptions
return true;
} catch (MalformedURLException e) {
e.printStackTrace();
return false;
} catch (ProtocolException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment