package com.andela.webservice;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Spykins on 08/06/16.
 */
public class HttpManager {

    public static String getData(RequestPackage p) {

        BufferedReader reader = null;
        String uri = p.getUri();
        //We need to append parameter only when we are making a get request
        //if it's any other request, we will use the parameter in a different way

        if(p.getMethod().equals("GET")) {
            uri += "?" + p.getEncodedParams();
        }

        try {
            URL url = new URL(uri);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            con.setRequestMethod(p.getMethod()); //This is value, GET, or POST
            StringBuilder sb = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            return sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }

    }

}
@@ -111,32 +111,67 @@
 utStreamReader;%0A
+import java.io.OutputStreamWriter;%0A
 import java.net.
@@ -185,24 +185,24 @@
 Connection;%0A
-
 import java.
@@ -790,25 +790,24 @@
 nnection();%0A
-%0A
             
@@ -873,16 +873,853 @@
 or POST%0A
+            //**** Note*** set the request method before writing the body%0A            if(p.getMethod().equals(%22POST%22)) %7B%0A                con.setDoOutput(true);%0A                // This allows to output content to the body of the request%0A                OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());%0A                //creating a write object that can write to the output stream and send information to the connection%0A                writer.write(p.getEncodedParams()); // This gets the same param as the Get, with the & and the = sign%0A                //all in the same place and the value encoded to send over the web... but instead of appending it to d uri%0A                //we will place it in the body of the request%0A                writer.flush(); //This flush anything written to the memory%0A            %7D%0A
         

Package the request as the request body to the POST request