简体   繁体   English

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

[英]Send string to a http server with JAVA

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);
    }

this code gives me a socket exception, please help I am at a lose 此代码给了我一个套接字异常,请帮助我迷路

This works for me in Java 8: 这在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();
        }

Make your life easier by using http://commons.apache.org/proper/commons-net/ 使用http://commons.apache.org/proper/commons-net/使您的生活更轻松

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

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