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

前端 未结 2 1375
故里飘歌
故里飘歌 2021-01-19 00:17

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, ar

2条回答
  •  悲哀的现实
    2021-01-19 01:00

    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:

    
        native
    
    
    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() { });
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    

提交回复
热议问题