Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过url.openStream()发送帖子数据?_Java - Fatal编程技术网

Java 如何通过url.openStream()发送帖子数据?

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")); 你能帮我吗?

我正在寻找教程或快速示例,如何通过openStream发送POST数据

我的代码是:

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回答问题而不是跳转到另一个库。