Http client and redirection

#1

Hi,
I’m starting a new project with MicroEJ and I need to embed an http client. I looked to the MicroEJ Github but I did not find any http client sample.
Then I found this library: http://developer.microej.com/4.1/ivy/artifacts/ej/library/eclasspath/httpclient/1.2.2/

Unfortunately, there is no mention of the list of implemented features.
I need support for automatic redirection (3xx code), does this version supports it ?

Thanks for you help,
Hugo

#2

Hi @hugo91,

You can use the HTTP Client which you show using the dependency:

<dependency org="ej.library.eclasspath" name="httpclient" rev="[1.2.2-RC0,2.0.0-RC0[" />

You can find the APIs here.

However, I would recommend you to use the rest client that will provide abstraction. You can find an example here using a JSONResource.

For instance, you can add the dependency:

<dependency org="ej.library.iot" name="restclient" rev="[1.0.5-RC0,2.0.0-RC0[" />

To get a text from an URL, you

String requestURL = "http://microej.com"; //$NON-NLS-1$
Resty resty = new Resty();

// do GET request request;
TextResource resource = resty.text(requestURL);
HttpURLConnection conn = resource.http();
try {
	int responseCode = conn.getResponseCode();
	System.out.println("Response code: "+responseCode );
	System.out.println(resource.toString());
} finally {
	conn.disconnect();
}

The REST Client library supports the redirect 3XX.

Regards,

#3

Hi @pierre.rivron,

Thank you for the quick and detailed reply.

Just to be clear with your last sentence:
[…] The REST Client library supports the redirect 3XX […]
Do you mean that the http client does not support it, is it correct ?

(the reason behind is that I have a limited amount of memory flash reserved for MicroEJ on my device, so I’m trying to limit the number of embedded libraries)

Hugo

#4

Hi @hugo91,

As mentioned in the APIs (look for HttpURLConnection), you can see that the setFollowRedirects method allows you to configure whether you want the 3xx codes to be followed or not.

Moreover, the default behavior is indeed to follow such redirects.

I hope this answers you question.

Best regards,
Thomas

#5

This is exactly what I was looking for.

Thank you @tvincent.

Hugo