Skip to content

Instantly share code, notes, and snippets.

@janxious
Last active October 8, 2015 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save janxious/3361577 to your computer and use it in GitHub Desktop.
Save janxious/3361577 to your computer and use it in GitHub Desktop.
DocRaptor Examples Set 3 - Non-Ruby (cont'd)
import java.io.*;
import java.net.*;
public class JavaExample {
public static void main(String[] args) throws Exception{
String document = "<html><body>This is a DocRaptor Example</body></html>";
String apikey = "YOUR_API_KEY_HERE";
String encoded_document = URLEncoder.encode(document, "UTF8");
String data = "doc[document_content]=" +encoded_document;
data += "&doc[name]=java_sample.pdf";
data += "&doc[document_type]=pdf";
data += "&doc[test]=true";
byte[] encodedData = data.getBytes("UTF8");
String url = "https://docraptor.com/docs?user_credentials=" + apikey;
String agent = "Mozilla/4.0";
String type = "application/x-www-form-urlencoded";
HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
conn.setDoOutput(true); // send as a POST
conn.setRequestProperty("User-Agent", agent);
conn.setRequestProperty("Content-Type", type);
conn.setRequestProperty("Content-Length", Integer.toString(encodedData.length));
OutputStream os = conn.getOutputStream();
os.write(encodedData);
os.flush();
InputStream responseStream = conn.getInputStream();
OutputStream outputStream = new FileOutputStream(new File("java_sample.pdf"));
byte[] buffer = new byte[1024];
int len;
while((len = responseStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
responseStream.close();
outputStream.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment