Java 如何通过url.openStream()发送帖子数据?
我正在寻找教程或快速示例,如何通过openStream发送POST数据 我的代码是:Java 如何通过url.openStream()发送帖子数据?,java,Java,我正在寻找教程或快速示例,如何通过openStream发送POST数据 我的代码是: 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("http://localhost:8080/test");
InputStream response = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(response, "UTF-8"));
你能帮我吗?使用Apache HTTP组件 教程:
查找HttpPost-这里有一些发送动态数据、文本、文件和表单数据的示例。使用Apache HTTP组件
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);
}
教程:
寻找HttpPost——这里有一些发送动态数据、文本、文件和表单数据的示例。尤其是,最好的方法是。
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);
}
它省去了很多你通常不得不手工编写的讨厌的代码,特别是,这将是最好的方法。
它省略了很多你通常必须手工完成的讨厌的编码回答问题而不是跳转到另一个库的可能重复的可能重复的+1。+1回答问题而不是跳转到另一个库。