【发布时间】:2021-05-19 20:09:12
【问题描述】:
从 Tomcat 上的 Java servlet,我将帖子发送到外部应用程序,该应用程序根据我在帖子中发送的“消息”启动“作业”(消息是从 html 表单发送的)。外部应用程序回复我的帖子说“作业已成功提交”或“作业未成功提交”。外部应用程序是一个烧瓶/python 应用程序,它启动“作业”,然后可以将更新发送回我的服务器。
我以后如何从外部应用接收关于作业进度或作业完成/失败的更新?
我正在使用 HttpURLConnection 将发布请求发送到外部应用程序:
public class Post extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
InputStream input = null;
try {
String message = new String();
// Data to be posted
String getMessage = request.getParameter("Message");
String USER_AGENT = "Mozilla/5.0";
String url1 = "http://xx.xx.xx.xx:8080/GetPost";
URL obj1 = new URL(url1);
HttpURLConnection con1 = (HttpURLConnection) obj1.openConnection();
// add request header
con1.setRequestMethod("POST");
con1.setRequestProperty("User-Agent", USER_AGENT);
con1.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
// Send post request
con1.setDoOutput(true);
DataOutputStream wr1 = new DataOutputStream(con1.getOutputStream());
wr1.writeBytes(getMessage);
wr1.flush();
wr1.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
虽然我确实阅读了帖子响应,(此处未包含代码)我不知道如何从外部应用程序接收以后的更新。接受所有建议,谢谢。
【问题讨论】:
标签: java servlets httpurlconnection