我在某个地方读到 goole 只允许获取。这是否意味着无法将 org.apache.http.client.HttpClient 集成到 google Appe 引擎中?
如果没有,是否有在 google app engine 上使用 org.apache.http.client.HttpClient 来使用现有库的替代方案?
最佳答案
2019 年更新
是的,你确实可以。我刚刚尝试过,它在 Java 8 环境中正常工作。
步骤:
启用billing,否则
native
HttpURLConnection
将无法工作(这也是Apache HttpClient 的基础)。如果不计费,您只能使用 2016 年上一篇博文中所述的旧版urlfetch
。在 Java 8 环境中可选,因为
native
是默认设置
appengine-web.xml
:
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<url-stream-handler>native</url-stream-handler>
</appengine-web-app>
- 编写您的代码,例如:
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);
}
关于java - 我可以在谷歌应用引擎中使用 org.apache.http.client.HttpClient 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16423338/