com.ecebs.rtd.enabler.common
Class HttpUtils

java.lang.Object
  extended by com.ecebs.rtd.enabler.common.HttpUtils

public class HttpUtils
extends Object

Simple class that provides methods to help in carrying out HTTP requests.

Author:
kyleb

Field Summary
static String CONTENT_TYPE_TEXT_PLAIN
          Default content type
 
Method Summary
static void checkValidAndThrow(HttpURLConnection con)
          Checks that the response code is equal to HttpURLConnection.HTTP_OK
static void copy(InputStream inStream, OutputStream outStream)
          Common helper method to copy from an input stream to an output stream
static byte[] read(URLConnection con)
          Reads the response from the connection
static String readString(URLConnection con)
          Reads the response data as a UTF-8 string.
static void write(URLConnection con, byte[] toWrite)
          Writes the specified data to the connection, closing the output stream when finished.
static void write(URLConnection con, byte[] toWrite, String contentType)
          Writes the specified data to the connection, closing the output stream when finished
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONTENT_TYPE_TEXT_PLAIN

public static final String CONTENT_TYPE_TEXT_PLAIN
Default content type

See Also:
Constant Field Values
Method Detail

checkValidAndThrow

public static void checkValidAndThrow(HttpURLConnection con)
                               throws IOException
Checks that the response code is equal to HttpURLConnection.HTTP_OK

Parameters:
con - The connection to check the response code of
Throws:
IOException - Thrown if there was an error getting a response code, or the response code was not HttpURLConnection.HTTP_OK

write

public static void write(URLConnection con,
                         byte[] toWrite)
                  throws IOException
Writes the specified data to the connection, closing the output stream when finished. This method is equavelent to calling write(URLConnection, byte[], String), passing in CONTENT_TYPE_TEXT_PLAIN as the content type.

Parameters:
con - Connection to write data to
toWrite - Data to write
Throws:
IOException - Thrown if there was an error writing the data

write

public static void write(URLConnection con,
                         byte[] toWrite,
                         String contentType)
                  throws IOException
Writes the specified data to the connection, closing the output stream when finished

Parameters:
con - Connection to write data to
toWrite - Data to write
contentType - content type of the data
Throws:
IOException - Thrown if there was an error writing the data

readString

public static String readString(URLConnection con)
                         throws IOException
Reads the response data as a UTF-8 string.

Parameters:
con - Connection to read the response from
Returns:
The response as a UTF-8 encoded string
Throws:
IOException - Thrown if there was an error while reading the response

read

public static byte[] read(URLConnection con)
                   throws IOException
Reads the response from the connection

Parameters:
con - Connection to read the response from
Returns:
read response
Throws:
IOException - Thrown if there was an error reading the response

copy

public static void copy(InputStream inStream,
                        OutputStream outStream)
                 throws IOException
Common helper method to copy from an input stream to an output stream

Parameters:
inStream - Stream to read from
outStream - Stream to write to
Throws:
IOException - Thrown if there was an error reading/writing


Copyright © 2013 Ecebs Ltd. All Rights Reserved.