发送POST并读取流式响应 [英] Send POST and read streaming response

查看:368
本文介绍了发送POST并读取流式响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器接收POST请求并使用数据流回答。我在 URL 上看到过,我可以打开连接或流。但是,流没有写出数据的方法:

I have a server that takes a POST request and answers with a data stream. I have seen that on URL I can open a connection or a stream. A stream, however, has no method for writing out data:

URL url = new URL("...");
url.openConnection(); //either I open a connection which has a output stream, but no input
url.openStream(); //or I open a stream, but I cannot write anything out

如何优雅地解决这个问题?

How can I solve this problem elegantly?

推荐答案

使用OutputStream的示例代码段。

Sample code snippet to use OutputStream.

注意:您可以设置内容类型&仅将URL参数发送到URL。

Note: You can set content types & send some URL parameters to the URL only.

    URL obj = new URL(url);//some url
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    String urlParams = "fName=xyz&lname=ABC&pin=12345"; // some parameters
    wr.writeBytes(urlParams);
    wr.flush();
    wr.close();

详细了解 article1 article2

这篇关于发送POST并读取流式响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆