mcbjam
mcbjam

Reputation: 7454

Can i use org.apache.http.client.HttpClient in google app engine?

I read somwhere that goole just allowed only fetch. Does that mean it's impossible to integrate org.apache.http.client.HttpClient in google appe engine ?

If not, are there an alternative for using existing librairies using org.apache.http.client.HttpClient on google app engine ?

Upvotes: 5

Views: 2906

Answers (2)

kavai77
kavai77

Reputation: 6626

Update 2019

Yes, you can, indeed. I just tried, it works without fail with the Java 8 environment.

Steps:

  1. Enable billing, otherwise the native HttpURLConnection won't work (which is also the basis of the Apache HttpClient). Without billing you can only use the legacy urlfetch as described in previous post from 2016.

  2. Optional in Java 8 environment, since native is the default

appengine-web.xml:

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <url-stream-handler>native</url-stream-handler>
</appengine-web-app>
  1. Write your code, e.g.:
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
    HttpPost httpPost = new HttpPost("http://myservice.com");
    httpPost.setEntity(new StringEntity(objectMapper.writeValueAsString(input), ContentType.APPLICATION_JSON));
    CloseableHttpResponse response = httpclient.execute(httpPost);
    return objectMapper.readValue(response.getEntity().getContent(), new TypeReference<MyReturnType>() { });
} catch (IOException e) {
    throw new RuntimeException(e);
}

Upvotes: 1

mcbjam
mcbjam

Reputation: 7454

So the answer is not. You need to use google fetch library.

From Google App Engine wiki page on Google Code as archived by Wayback Machine:

The only supported HTTP transport is UrlFetchTransport based on URL Fetch Java API in the Google App Engine SDK

Do not try ApacheHttpTransport because it will definitely fail on Google App Engine.

Upvotes: 0

Related Questions