无法从 applet 向 servlet 发送请求
我有以下代码:
URL urlServlet = new URL(WEB_SERVER_URL);
URLConnection connection = urlServlet.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setDefaultUseCaches(true);
connection.setRequestProperty("Content-Type", "application/octet-stream");
connection.setRequestProperty("event", "blah");
OutputStream outputStream = servletConnection.getOutputStream();
outputStream.flush();
outputStream.close();
服务器没有响应该程序。
但是,如果我从连接获取 inputStream,我会在 DoGet servlet 方法中捕获断点。
我做错了什么?
I have the following code:
URL urlServlet = new URL(WEB_SERVER_URL);
URLConnection connection = urlServlet.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setDefaultUseCaches(true);
connection.setRequestProperty("Content-Type", "application/octet-stream");
connection.setRequestProperty("event", "blah");
OutputStream outputStream = servletConnection.getOutputStream();
outputStream.flush();
outputStream.close();
The server is not responding to this program.
But if I get inputStream from connection, I catch breakpoint in the DoGet servlet method.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的错误是没有请求响应。
URLConnection
是延迟执行的。仅当您请求响应时才会发送请求。调用getInputStream()
实际上会触发 HTTP 请求,因为您正在请求响应。当您只是打开和关闭请求正文时,不会建立连接。另请参阅:
Your mistake was to not asking for the response. The
URLConnection
is lazily executed. The request will only be sent whenever you ask for the response. CallinggetInputStream()
will actually fire the HTTP request because you're asking for the response. The connection will not be made when you just open and close the request body.See also: