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

此代碼給了我一個套接字異常,請幫助我迷路

這在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/使您的生活更輕松

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM