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

我正在寻找教程或快速示例,我如何发送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);
    }

使用Apache HTTP Compoennts http://hc.apache.org/httpcomponents-client-ga/

教程:http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html

寻找HttpPost - 有一些发送动态数据,文本,文件和表单数据的例子。


尤其是Apache HTTP组件,客户端将是最好的选择。 它可以避免许多你通常需要手工完成的令人讨厌的编码

链接地址: http://www.djcxy.com/p/45849.html

上一篇: How can I send POST data through url.openStream()?

下一篇: HTTP multipart classes in java