Skip to content

Instantly share code, notes, and snippets.

@janxious
Last active October 8, 2015 16:58

Revisions

  1. janxious revised this gist Jan 29, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion JavaExample.java
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,8 @@ 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 data = "doc[document_content]=" + document;
    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";
  2. janxious revised this gist Aug 16, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion JavaExample.java
    Original file line number Diff line number Diff line change
    @@ -36,4 +36,4 @@ public static void main(String[] args) throws Exception{
    responseStream.close();
    outputStream.close();
    }
    }
    }
  3. janxious created this gist Aug 15, 2012.
    39 changes: 39 additions & 0 deletions JavaExample.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    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 data = "doc[document_content]=" + 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();
    }
    }