本文介绍了发送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并读取流式响应
发布评论