我正在尝试通过Apache HttpClient从Servlet发送一个简单的Hello World到Slack Webhook。最后,我的servlet将作为JIRA的Webhook并针对“ Jira Server Alerts”不支持的某些事件向Slack发布消息。
现在发生了一些有趣的事情:每当我运行doPost()方法时,我都会从Slack收到invalid_payload-Reply。但是,如果我使用本地Postman-proxy拦截了请求,则该请求将被接受并发布消息。代理与我的HttpClient有何不同?
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
final HttpClient hc = HttpClientBuilder.create()
.setRedirectStrategy(new LaxRedirectStrategy())
// .setProxy(new HttpHost("localhost", 5555))
.build();
final HttpHost target = new HttpHost("hooks.slack.com");
final HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST",
"<Slack Webhook URL>");
request.setHeader("Content-Type", "application/json");
request.setEntity(new StringEntity("{ \"text\" : \"Hello World\" }",
ContentType.APPLICATION_JSON));
System.err.println(request.getRequestLine());
for(Header h : request.getAllHeaders()) {
System.err.println(h);
}
System.err.println(EntityUtils.toString(request.getEntity()));
System.err.println("------------------------------------------------");
try {
final HttpResponse response = hc.execute(target, request);
System.err.println(response.getStatusLine());
System.err.println(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}
resp.setStatus(200);
}