HttpHelper

public class HttpHelper
extends Object implements IHttpHelper

java.lang.Object
   ↳ com.android.tradefed.util.net.HttpHelper


Contains helper methods for making http requests

Summary

Nested classes

class HttpHelper.RequestRunnable

Runnable for making requests with runEscalatingTimedRetry(long, long, long, long, IRunnableResult)

Public constructors

HttpHelper()

Public methods

String buildParameters(MultiMap<String, String> paramMap)

Build the encoded parameter string.

String buildUrl(String baseUrl, MultiMap<String, String> paramMap)

Build the full encoded URL request string.

HttpURLConnection createConnection(URL url, String method, String contentType)

Create a to given url.

HttpURLConnection createJsonConnection(URL url, String method)

Creates a connection to given URL for passing json data.

HttpURLConnection createXmlConnection(URL url, String method)

Creates a connection to given URL for passing xml data.

String doGet(String url)

Performs a GET HTTP request method for a given URL and returns it as a String.

void doGet(String url, OutputStream outputStream)

Performs a GET HTTP request method for a given URL and streams result to a OutputStream.

void doGetIgnore(String url)

Performs a GET for a given URL, with the given URL parameters ignoring the result.

void doGetIgnoreWithRetry(String url)

Performs {doGetIgnore(String) retrying upon failure.

String doGetWithRetry(String url)

Performs {doGet(String) retrying upon failure.

String doPostWithRetry(String url, String postData)

Performs a POST HTTP request method for a given URL and returns it as a String, retrying upon failure.

String doPostWithRetry(String url, String postData, String contentType)

Performs a POST HTTP request method for a given URL and returns it as a String, retrying upon failure.

int getInitialPollInterval()

Get the initial poll interval in ms.

int getMaxPollInterval()

Get the max poll interval in ms.

int getMaxTime()

Get the maximum time to keep trying the request in ms.

int getOpTimeout()

Get the operation timeout in ms.

IRunUtil getRunUtil()

Get IRunUtil to use.

void setInitialPollInterval(int time)

Set the initial poll interval in ms.

void setMaxPollInterval(int time)

Set the initial poll interval in ms.

void setMaxTime(int time)

Set the maximum time to keep trying the request in ms.

void setOpTimeout(int time)

Set the operation timeout in ms.

Public constructors

HttpHelper

HttpHelper ()

Public methods

buildParameters

String buildParameters (MultiMap<String, String> paramMap)

Build the encoded parameter string.

Parameters
paramMap MultiMap: the URL parameters

Returns
String the encoded parameter string

buildUrl

String buildUrl (String baseUrl, 
                MultiMap<String, String> paramMap)

Build the full encoded URL request string.

Parameters
baseUrl String: the base URL

paramMap MultiMap: the URL parameters

Returns
String the constructed URL

createConnection

HttpURLConnection createConnection (URL url, 
                String method, 
                String contentType)

Create a to given url.

Parameters
url URL: the URL to connect to.

method String: the HTTP request method. For example, GET or POST.

contentType String: the content type. For example, "text/html".

Returns
HttpURLConnection The HttpURLConnection

Throws
IOException

createJsonConnection

HttpURLConnection createJsonConnection (URL url, 
                String method)

Creates a connection to given URL for passing json data.

Parameters
url URL: the URL to connect to.

method String: the HTTP request method. For example, GET or POST.

Returns
HttpURLConnection the HttpURLConnection

Throws
IOException

createXmlConnection

HttpURLConnection createXmlConnection (URL url, 
                String method)

Creates a connection to given URL for passing xml data.

Parameters
url URL: the URL to connect to.

method String: the HTTP request method. For example, GET or POST.

Returns
HttpURLConnection the HttpURLConnection

Throws
IOException

doGet

String doGet (String url)

Performs a GET HTTP request method for a given URL and returns it as a String.

Because remote contents are loaded into memory, this method should only be used for relatively small data sizes.

References:

Parameters
url String: the URL

Returns
String the String remote contents

Throws
IOException
IHttpHelper.DataSizeException

doGet

void doGet (String url, 
                OutputStream outputStream)

Performs a GET HTTP request method for a given URL and streams result to a OutputStream.

Parameters
url String: the URL

outputStream OutputStream: stream of the response data

Throws
IOException

doGetIgnore

void doGetIgnore (String url)

Performs a GET for a given URL, with the given URL parameters ignoring the result.

Parameters
url String: the URL

Throws
IOException

doGetIgnoreWithRetry

void doGetIgnoreWithRetry (String url)

Performs {doGetIgnore(String) retrying upon failure.

Parameters
url String: the URL

Throws
IOException

doGetWithRetry

String doGetWithRetry (String url)

Performs {doGet(String) retrying upon failure.

Parameters
url String: the URL

Returns
String the String remote contents

Throws
IOException
IHttpHelper.DataSizeException

doPostWithRetry

String doPostWithRetry (String url, 
                String postData)

Performs a POST HTTP request method for a given URL and returns it as a String, retrying upon failure.

Because remote contents are loaded into memory, this method should only be used for relatively small data sizes.

Parameters
url String: the URL

postData String: the data to be posted once the connection is open

Returns
String the String remote contents

Throws
IOException
IHttpHelper.DataSizeException

doPostWithRetry

String doPostWithRetry (String url, 
                String postData, 
                String contentType)

Performs a POST HTTP request method for a given URL and returns it as a String, retrying upon failure.

Because remote contents are loaded into memory, this method should only be used for relatively small data sizes.

Parameters
url String: the URL

postData String: the data to be posted once the connection is open

contentType String: the content type. For example, "text/html".

Returns
String the String remote contents

Throws
IOException
IHttpHelper.DataSizeException

getInitialPollInterval

int getInitialPollInterval ()

Get the initial poll interval in ms.

Returns
int

getMaxPollInterval

int getMaxPollInterval ()

Get the max poll interval in ms.

Returns
int

getMaxTime

int getMaxTime ()

Get the maximum time to keep trying the request in ms.

Returns
int

getOpTimeout

int getOpTimeout ()

Get the operation timeout in ms.

Returns
int

getRunUtil

IRunUtil getRunUtil ()

Get IRunUtil to use. Exposed so unit tests can mock.

Returns
IRunUtil

setInitialPollInterval

void setInitialPollInterval (int time)

Set the initial poll interval in ms.

Parameters
time int

setMaxPollInterval

void setMaxPollInterval (int time)

Set the initial poll interval in ms.

Parameters
time int

setMaxTime

void setMaxTime (int time)

Set the maximum time to keep trying the request in ms.

Parameters
time int

setOpTimeout

void setOpTimeout (int time)

Set the operation timeout in ms.

Parameters
time int