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 IRunUtil#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.

void doGet(String url, OutputStream outputStream)

Performs a GET HTTP request method for a given URL and streams result to a ERROR(/OutputStream).

String doGet(String url)

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

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

public HttpHelper ()

Public methods

buildParameters

public String buildParameters (MultiMap<String, String> paramMap)

Build the encoded parameter string.

Parameters
paramMap MultiMap: the URL parameters

Returns
String the encoded parameter string

buildUrl

public 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

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

Create a to given url.

Parameters
url URL: the ERROR(/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

createJsonConnection

public HttpURLConnection createJsonConnection (URL url, 
                String method)

Creates a connection to given URL for passing json data.

Parameters
url URL: the ERROR(/URL) to connect to.

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

Returns
HttpURLConnection the ERROR(/HttpURLConnection)

createXmlConnection

public HttpURLConnection createXmlConnection (URL url, 
                String method)

Creates a connection to given URL for passing xml data.

Parameters
url URL: the ERROR(/URL) to connect to.

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

Returns
HttpURLConnection the ERROR(/HttpURLConnection)

doGet

public void doGet (String url, 
                OutputStream outputStream)

Performs a GET HTTP request method for a given URL and streams result to a ERROR(/OutputStream).

Parameters
url String: the URL

outputStream OutputStream: stream of the response data

doGet

public 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
IHttpHelper.DataSizeException

doGetIgnore

public void doGetIgnore (String url)

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

Parameters
url String: the URL

doGetIgnoreWithRetry

public void doGetIgnoreWithRetry (String url)

Performs {doGetIgnore(String) retrying upon failure.

Parameters
url String: the URL

doGetWithRetry

public String doGetWithRetry (String url)

Performs {doGet(String) retrying upon failure.

Parameters
url String: the URL

Returns
String the String remote contents

Throws
IHttpHelper.DataSizeException

doPostWithRetry

public 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
IHttpHelper.DataSizeException

doPostWithRetry

public 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
IHttpHelper.DataSizeException

getInitialPollInterval

public int getInitialPollInterval ()

Get the initial poll interval in ms.

Returns
int

getMaxPollInterval

public int getMaxPollInterval ()

Get the max poll interval in ms.

Returns
int

getMaxTime

public int getMaxTime ()

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

Returns
int

getOpTimeout

public int getOpTimeout ()

Get the operation timeout in ms.

Returns
int

getRunUtil

public IRunUtil getRunUtil ()

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

Returns
IRunUtil

setInitialPollInterval

public void setInitialPollInterval (int time)

Set the initial poll interval in ms.

Parameters
time int

setMaxPollInterval

public void setMaxPollInterval (int time)

Set the initial poll interval in ms.

Parameters
time int

setMaxTime

public void setMaxTime (int time)

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

Parameters
time int

setOpTimeout

public void setOpTimeout (int time)

Set the operation timeout in ms.

Parameters
time int