如何管理与服务器的请求-响应对话循环
我正在编写一个简单的客户端-服务器系统,问题是:如何构建我的客户端代码以使 POST 请求-响应循环工作?
目前它看起来像这样(而且现在不是循环):
- open HttpURLConnection
- set properties
- setDoOutput(true)
- writing to output flow
- closeoutputstream
- new DataInputStreamreading
- response
- exiting method
我不确定我有哪些对象为下一次迭代保存以及我应该关闭哪些迭代。
I'm writing a simple client-server system and the question is: how to structure my client code in order to get POST request-response working in a loop?
At the moment it looks something like this (and it's is NOT a loop right now):
- open HttpURLConnection
- set properties
- setDoOutput(true)
- writing to output stream
- closing output stream
- new DataInputStream
- reading response
- exiting method
I'm not sure which objects do I have to save for the next iterations and which ones I should close.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要保存连接对象,并且应该使用
setDoInput(true)
来读取数据,但如果您只想读取responseCode
和responseMessage
> 你不需要InputStream
。检查下面的代码。对于
InputStreamReader
you need to save the connection object and you should make use of
setDoInput(true)
for reading data but if you just want to readresponseCode
andresponseMessage
you dont needInputStream
. check the code below.for
InputStreamReader
在管理了我自己关于这个主题的“研究”之后(感谢谷歌和诺基亚开发者论坛),我得出了我的代码的最终视图。这是一个文件上传循环:
After managing my own 'research' on this subject (thanks to Google and Nokia Developer forums) I've come to the final view of my code. It's a file upload loop: