gpt4 book ai didi

java - 如何通过 url.openStream() 发送 POST 数据?

转载 作者:行者123 更新时间:2023-11-29 07:16:51 25 4
gpt4 key购买 nike

我正在寻找教程或快速示例,了解如何将 POST 数据发送到 openStream。

我的代码是:

URL url = new URL("http://localhost:8080/test");
InputStream response = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(response, "UTF-8"));

你能帮帮我吗?

最佳答案

    URL url = new URL(urlSpec);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.setDoInput(true);

// important: get output stream before input stream
OutputStream out = connection.getOutputStream();
out.write(content);
out.close();

// now you can get input stream and read.
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = null;

while ((line = reader.readLine()) != null) {
writer.println(line);
}

关于java - 如何通过 url.openStream() 发送 POST 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8804511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com