繁体   English   中英

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

[英]How can I send POST data through url.openStream()?

我正在寻找教程或快速示例,我如何发送 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 组件http://hc.apache.org/httpcomponents-client-ga/

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

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

特别是Apache HTTP 组件客户端将是最好的方法。 它抽象了您通常必须手动完成的许多讨厌的编码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM