发送POST并读取流式响应

编程入门 行业动态 更新时间:2024-10-28 18:32:58
本文介绍了发送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并读取流式响应

本文发布于:2023-10-10 11:20:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478519.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:流式   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!