bspkrs

stor.ag code

Aug 23rd, 2013
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. package rs.bspk.drunkenadventure;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.HttpURLConnection;
  7. import java.net.MalformedURLException;
  8. import java.net.ProtocolException;
  9. import java.net.URL;
  10.  
  11. public class DrunkenAdventureMain
  12. {
  13.    
  14.     /**
  15.      * @param args
  16.      */
  17.     public static void main(String[] args)
  18.     {
  19.         byte[] bytesData;//byte array with file data
  20.        
  21.         URL url;
  22.         try
  23.         {
  24.             url = new URL("http://api.stor.ag/v2/cfs/");
  25.            
  26.             //Configure connection
  27.             HttpURLConnection conn;
  28.             try
  29.             {
  30.                 conn = (HttpURLConnection) url.openConnection();
  31.                 conn.setDoOutput(true);
  32.                 try
  33.                 {
  34.                     conn.setRequestMethod("POST");
  35.                 }
  36.                 catch (ProtocolException e)
  37.                 {
  38.                     // TODO Auto-generated catch block
  39.                     e.printStackTrace();
  40.                 }
  41.                 conn.setReadTimeout(60000);
  42.                
  43.                 //Set headers
  44.                 conn.setRequestProperty("Storage-Key", "REDACTED");
  45.                 conn.setRequestProperty("CFS-name", "test");
  46.                
  47.                 System.out.println(conn.getResponseCode() + " --- " + conn.getResponseMessage());
  48.                
  49.                 //Check connection
  50.                 //                if (conn.getResponseCode() != HttpURLConnection.HTTP_OK)
  51.                 //                {
  52.                 //                    throw new RuntimeException("FAILED!!! HTTP error code: " +
  53.                 //                            conn.getResponseCode() + " --- " +
  54.                 //                            conn.getResponseMessage());
  55.                 //                }
  56.                
  57.                 //Get response
  58.                 System.out.println("\nGetting response...");
  59.                 BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  60.                
  61.                 //Print response
  62.                 System.out.println("\nOutput from Server .... \n");
  63.                 String output;
  64.                 while ((output = br.readLine()) != null)
  65.                 {
  66.                     System.out.println(output);
  67.                 }
  68.             }
  69.             catch (IOException e1)
  70.             {
  71.                 // TODO Auto-generated catch block
  72.                 e1.printStackTrace();
  73.             }
  74.         }
  75.         catch (MalformedURLException e)
  76.         {
  77.             // TODO Auto-generated catch block
  78.             e.printStackTrace();
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment