如何管理与服务器的请求-响应对话循环

发布于 2024-12-24 01:54:39 字数 370 浏览 0 评论 0原文

我正在编写一个简单的客户端-服务器系统,问题是:如何构建我的客户端代码以使 POST 请求-响应循环工作?

目前它看起来像这样(而且现在不是循环):

  1. op​​en HttpURLConnection
  2. set properties
  3. setDoOutput(true)
  4. writing to output flow
  5. closeoutputstream
  6. new DataInputStreamreading
  7. response
  8. 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):

  1. open HttpURLConnection
  2. set properties
  3. setDoOutput(true)
  4. writing to output stream
  5. closing output stream
  6. new DataInputStream
  7. reading response
  8. exiting method

I'm not sure which objects do I have to save for the next iterations and which ones I should close.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

硪扪都還晓 2024-12-31 01:54:39

您需要保存连接对象,并且应该使用 setDoInput(true) 来读取数据,但如果您只想读取 responseCoderesponseMessage > 你不需要InputStream。检查下面的代码。

HttpURLConnection connection =(HttpURLConnection)new URL("url").openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-type", "text/xml"); // depend on you
connection.setRequestProperty("Accept", "text/xml, application/xml"); // depend on you
connection.setRequestMethod("POST");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(yaml);
writer.close();
int statusCode = connection.getResponseCode();
String message = connection.getResponseMessage();

对于InputStreamReader

connection.setDoInput(true);
InputStreamReader reader =  new InputStreamReader(connection.getInputStream());
char[] cbuf = new char[100];
reader.read(cbuf); 
// there are 3 read method you can choose as per your convenience 
//and put a check for end of line in while loop for reading whole content. 
reader.close();

you need to save the connection object and you should make use of setDoInput(true) for reading data but if you just want to read responseCode and responseMessage you dont need InputStream. check the code below.

HttpURLConnection connection =(HttpURLConnection)new URL("url").openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-type", "text/xml"); // depend on you
connection.setRequestProperty("Accept", "text/xml, application/xml"); // depend on you
connection.setRequestMethod("POST");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(yaml);
writer.close();
int statusCode = connection.getResponseCode();
String message = connection.getResponseMessage();

for InputStreamReader

connection.setDoInput(true);
InputStreamReader reader =  new InputStreamReader(connection.getInputStream());
char[] cbuf = new char[100];
reader.read(cbuf); 
// there are 3 read method you can choose as per your convenience 
//and put a check for end of line in while loop for reading whole content. 
reader.close();
独自←快乐 2024-12-31 01:54:39

在管理了我自己关于这个主题的“研究”之后(感谢谷歌和诺基亚开发者论坛),我得出了我的代码的最终视图。这是一个文件上传循环:

path = Paths.get(requestString);
in = Files.newInputStream(path);

int i = 0;
while ((bytesRead = in.read(buf)) != -1) { 
    URL u = new URL(defaultURL);
    huc = 
        (HttpURLConnection) u.openConnection();
    huc.setRequestMethod("POST");
    huc.setDoOutput(true);
    huc.setDoInput(true);

    os = huc.getOutputStream();
    os.write(buf, 0, bytesRead);
    os.flush();
    os = null;

    // thanks to dku.rajkumar for the following block of code ! 
    InputStreamReader reader =  
        new InputStreamReader(huc.getInputStream());
    char[] cbuf = new char[400];
    reader.read(cbuf);
    reader.close();

    String s = new String(cbuf);
    messagebuffer.append(s + "\n\n");

    huc.disconnect();

    Thread.sleep(16);
}

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:

path = Paths.get(requestString);
in = Files.newInputStream(path);

int i = 0;
while ((bytesRead = in.read(buf)) != -1) { 
    URL u = new URL(defaultURL);
    huc = 
        (HttpURLConnection) u.openConnection();
    huc.setRequestMethod("POST");
    huc.setDoOutput(true);
    huc.setDoInput(true);

    os = huc.getOutputStream();
    os.write(buf, 0, bytesRead);
    os.flush();
    os = null;

    // thanks to dku.rajkumar for the following block of code ! 
    InputStreamReader reader =  
        new InputStreamReader(huc.getInputStream());
    char[] cbuf = new char[400];
    reader.read(cbuf);
    reader.close();

    String s = new String(cbuf);
    messagebuffer.append(s + "\n\n");

    huc.disconnect();

    Thread.sleep(16);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文