使用JAVA将字符串发送到http服务器

时间:2014-12-10 00:39:28

标签: java string http

try {
        URL  url = new URL("ftp://username:password@url.net/FileToWrite.txt");
        URLConnection urlc = url.openConnection();
        OutputStream os = urlc.getOutputStream();
        OutputStream buffer = new BufferedOutputStream(os);
        ObjectOutput output = new ObjectOutputStream(buffer);
        output.writeChars("hello");
        buffer.close();
        os.close();
        output.close();
    } catch (IOException e) {
        System.out.println(e);
    }

这段代码给了我一个套接字异常,请帮助我输掉

1 个答案:

答案 0 :(得分:0)

这适用于Java 8:

        try {
            URL  url = new URL("http://cnn.com"); /* Tested on a real URL */
            URLConnection urlc = url.openConnection();
urlc.setDoOutput(true); /* Added this ... */
            OutputStream os = urlc.getOutputStream();
            OutputStream buffer = new BufferedOutputStream(os);
            ObjectOutput output = new ObjectOutputStream(buffer);
            output.writeChars("hello");
            buffer.close();
            os.close();
            output.close();
            System.out.println("Done");
        } catch (IOException e) {
            e.printStackTrace();
        }

使用http://commons.apache.org/proper/commons-net/

让您的生活更轻松