Skip to content

Instantly share code, notes, and snippets.

@chinmo
Last active December 17, 2015 05:39
Show Gist options
  • Save chinmo/5559724 to your computer and use it in GitHub Desktop.
Save chinmo/5559724 to your computer and use it in GitHub Desktop.
標準的なjavaライブラリだけでredmineにissue作る
import java.io.*;
import java.net.*;
public class Test {
public static void main(String [] args) {
try {
String content="{\"issue\": {\"project_id\": 1, \"subject\": \"Example\"}}";
log(content);
URL url = new URL("(redmineのURI)/issues.json");
HttpAuthenticator http_authenticator = new HttpAuthenticator("username", "password");
Authenticator.setDefault(http_authenticator);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type", "application/json");
connection.setRequestProperty("Content-length", Integer.toString(content.length()));
connection.setDoOutput(true);
connection.setDoInput(true);
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(content);
out.flush();
out.close();
connection.disconnect();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while (null != (line = in.readLine())) {
log(line);
}
} catch(Exception e) {
e.printStackTrace();
}
}
private static void log(String message) {
System.out.println(message);
}
}
class HttpAuthenticator extends Authenticator {
private String username;
private String password;
public HttpAuthenticator(String username, String password){
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username, password.toCharArray());
}
public String myGetRequestingPrompt(){
return super.getRequestingPrompt();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment