Skip to content

Instantly share code, notes, and snippets.

@scashin133
Created November 2, 2011 02:07
Show Gist options
  • Save scashin133/1332660 to your computer and use it in GitHub Desktop.
Save scashin133/1332660 to your computer and use it in GitHub Desktop.
// Java example
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.HttpURLConnection;
public class Api {
static final String kuser = "emily@socialcast.com";
static final String kpass = "demo";
static class SocialcastAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication(kuser, kpass.toCharArray()));
}
}
public static void main(String[] args) throws java.net.MalformedURLException, java.io.IOException{
Authenticator.setDefault(new SocialcastAuthenticator());
URL url = new URL("https://demo.socialcast.com/api/messages.xml");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
String data = "message[title]=trying%20out%20the%20api&message[body]=hello&message[attachment_ids][]=120&message[attachment_ids][]=121";
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write(data);
writer.flush();
InputStream ins = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String str;
while((str = reader.readLine()) != null)
System.out.println(str);
reader.close();
writer.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment