JavaからTwitterに投稿するプログラム
※20091126間違っていたので直しました
import java.net.*; import java.io.*; import java.net.Authenticator; import java.net.PasswordAuthentication; public class Twitter { public static void main(String[] args) throws Exception { String username = args[0]; String password = args[1]; String message = args[2]; HttpAuthenticator http_authenticator = new HttpAuthenticator(username, password); Authenticator.setDefault(http_authenticator); URL url = new URL("https://twitter.com/statuses/update.xml"); HttpURLConnection con = (HttpURLConnection)(url.openConnection()); con.setRequestMethod("POST"); con.setDoOutput(true); con.connect(); PrintWriter out = new PrintWriter(con.getOutputStream()); String message_str = "status=" + URLEncoder.encode(message,"UTF-8"); out.print(message_str); out.close(); if(con.getResponseCode()!=HttpURLConnection.HTTP_OK) System.out.println(con.getResponseCode()); con.disconnect(); } } 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(); } }