public class

HttpRequest

extends Object
java.lang.Object
   ↳ com.neomades.io.http.HttpRequest

Class Overview

HttpRequest object represents an HTTP/HTTPS request.

Configuration for a simple request

To use this class, the INTERNET permission must be declared in the URS file.

Example: basic request to neomades.com

 HttpRequest request = new HttpRequest("http://www.neomades.com").setMethodGet();
 request.executeAsync(this);
 

By default, the HTTP method used is the GET.

ATS (App Transport Security) for iOS

HTTP connections in the application must follow the ATS requirements:

  • The HTTP connection must use HTTPS
  • The negotiated Transport Layer Security (TLS) version of the contacted server must be TLS 1.2.
  • The digital certificate of the server must follow the X. 509 norm and must be trusted by a certificate authority.
  • The leaf server certificate must be signed with RSA or ECC keys.
  • The connection must use either the AES-128 or AES-256 symmetric cipher.

If a connection to a back end server cannot fulfill all the requirements, ATS can be bypassed by configuring the URS of the project. However, in that case, you will have to justify this in iTunes Connect when the application will be prepared for publication and Apple may reject the application.

Full documentation about ATS and network security are available at NeoMAD documentation.

Summary

Public Constructors
HttpRequest(String url)
Creates a new HTTP/HTTPS request.
Public Methods
void cancel()
Cancels or Closes the current opened/opening connection.
HttpResponse execute()
Opens the connection requested.
void executeAsync()
Opens the connection requested.
void executeAsync(HttpListener listener)
Opens the connection requested.
String getEncoding()
Returns the specified encoding or null.
int getTimeout()
Returns the timeout.
String getUrl()
Returns the URL of this request.
HttpRequest setDownloadProgressListener(HttpProgressListener progressListener)
Sets a download progress listener.
HttpRequest setEncoding(String encoding)
Sets the encoding that will be used when sending data with the POST method.
HttpRequest setHeader(String name, String value)
Insert HTTP header property.
HttpRequest setMethod(String method)
Set the method for the URL request, one of:
  • GET
  • POST
  • HEAD
  • OPTIONS
  • PUT
  • PATCH
  • DELETE
  • TRACE
are legal, subject to protocol restrictions.
HttpRequest setMethodGet()
Enables the HTTP GET method.
HttpRequest setMethodHead()
Enables the HTTP HEAD method.
HttpRequest setMethodPost()
Enables the HTTP POST method.
HttpRequest setPostContent(InputStream content)
Insert HTTP POST content.
HttpRequest setPostContent(byte[] content)
Insert HTTP POST content.
HttpRequest setPostParameter(String name, String value)
Insert or replace an existing HTTP POST parameter.
HttpRequest setTimeout(int timeout)
Sets a timeout for the connection (milliseconds) for waiting for data.
HttpRequest setUploadProgressListener(HttpProgressListener progressListener)
Sets an upload progress listener.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public HttpRequest (String url)

Creates a new HTTP/HTTPS request.

After creating the request, you could set options of the request by calling setXXXXX methods. When the request object is ready, call the execute(), executeAsync() or executeAsync(HttpListener) methods, to create connection and begin to connect to the server.

By default, the HTTP method used is the GET.

Parameters
url an HTTP/HTTPS URL

Public Methods

public void cancel ()

Cancels or Closes the current opened/opening connection.

public HttpResponse execute ()

Opens the connection requested. This method is synchronous.

Using this method is not recommended. Prefer executeAsync() or executeAsync(HttpListener) instead.

Cross Platform considerations

Since Android API level 11 this method cannot be called in the main-thread, a CrossThreadException will be thrown.

Returns
  • the response to this request
Throws
CrossThreadException if the method is called in the main-thread in Android
SecurityException if the INTERNET permission is not declared in the URS File
RuntimeException

public void executeAsync ()

Opens the connection requested. This method is asynchronous.

Throws
SecurityException if the INTERNET permission is not declared in the URS File

public void executeAsync (HttpListener listener)

Opens the connection requested. This method is asynchronous.

Parameters
listener the HTTP response listener
Throws
SecurityException if the INTERNET permission is not declared in the URS File

public String getEncoding ()

Returns the specified encoding or null.

Returns
  • the specified encoding or null.

public int getTimeout ()

Returns the timeout.

Returns
  • the timeout.

public String getUrl ()

Returns the URL of this request.

Returns
  • the URL of this request.

public HttpRequest setDownloadProgressListener (HttpProgressListener progressListener)

Sets a download progress listener.

Be careful : The progress download listener could work only if a Content-Length is set by the server and could be read by the Http client.

Parameters
progressListener a listener called when the download has progressed.
Returns
  • the current request

public HttpRequest setEncoding (String encoding)

Sets the encoding that will be used when sending data with the POST method. It is recommended to call this method, or else the data will be send with the platform's default encoding. This varies from one platform to another.

Parameters
encoding the encoding to use
Returns
  • the current request

public HttpRequest setHeader (String name, String value)

Insert HTTP header property. If the header name is already defined, we replace the previous value with the given parameter.
Headers with null name or value are ignored.

Parameters
name name of the HTTP head property
value value of the HTTP head property
Returns
  • the current request

public HttpRequest setMethod (String method)

Set the method for the URL request, one of:

  • GET
  • POST
  • HEAD
  • OPTIONS
  • PUT
  • PATCH
  • DELETE
  • TRACE
are legal, subject to protocol restrictions.

Parameters
method String: the HTTP method
Returns
  • the current request
Throws
ProtocolException if the requested method isn't valid for HTTP.

public HttpRequest setMethodGet ()

Enables the HTTP GET method.

Returns
  • the current request

public HttpRequest setMethodHead ()

Enables the HTTP HEAD method.

Returns
  • the current request

public HttpRequest setMethodPost ()

Enables the HTTP POST method.

Returns
  • the current request

public HttpRequest setPostContent (InputStream content)

Insert HTTP POST content. This method is NOT mixed with setPostParameter(String, String) or setPostContent(byte[]). Calling this method will erase the post parameters already inserted. If the current method is GET, the content will be ignored.

Parameters
content InputStream with the data to post
Returns
  • the current request

public HttpRequest setPostContent (byte[] content)

Insert HTTP POST content. This method is NOT mixed with setPostParameter(String, String) or setPostContent(InputStream). Calling this method will erase the post parameters already inserted. If the current method is GET, the content will be ignored.

Parameters
content byte array with the data to post
Returns
  • the current request

public HttpRequest setPostParameter (String name, String value)

Insert or replace an existing HTTP POST parameter.

This method can be call several times to insert multiple POST parameters.

This method is NOT mixed with setPostContent(byte[]) or setPostContent(InputStream). Calling this method will erase the post content already inserted.

If the current method is GET, the parameters will be ignored.

Parameters
name name of the HTTP POST parameter
value value of the HTTP POST parameter
Returns
  • the current request

public HttpRequest setTimeout (int timeout)

Sets a timeout for the connection (milliseconds) for waiting for data.

By default, a timeout of 10 seconds is set.

A timeout value of zero is interpreted as an infinite timeout.

If the timeout has been reached, the HttpResponse will be returned (depends on synchronous or asynchronous methods).

Parameters
timeout time out in milliseconds
Returns
  • the current request

public HttpRequest setUploadProgressListener (HttpProgressListener progressListener)

Sets an upload progress listener.

The upload is realized with POST HTTP requests.

Parameters
progressListener a listener called when the upload has progressed.
Returns
  • the current request