本文介绍了如何通过url.openStream()发送POST数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm looking for tutorial or quick example, how i can send POST data throw 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);
    }

这篇关于如何通过url.openStream()发送POST数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 15:36