集册 Java实例教程 从URL读取内容字符串

从URL读取内容字符串

欢马劈雪     最近更新时间:2020-01-02 10:19:05

487
从URL读取内容字符串

import java.io.*;//NowJava.com 提供

import java.net.*;


public class Reverse {

    public static void main(String[] args) throws Exception {


        if (args.length != 2) {

            System.err.println("Usage:  java Reverse "

                    + "http://<location of your servlet/script>"

                    + " string_to_reverse");

            System.exit(1);

        }


        String stringToReverse = URLEncoder.encode(args[1], "UTF-8");


        URL url = new URL(args[0]);

        URLConnection connection = url.openConnection();

        connection.setDoOutput(true);


        OutputStreamWriter out = new OutputStreamWriter(

                connection.getOutputStream());

        out.write("string=" + stringToReverse);/*来 自 nowjava - 时  代  Java*/

        out.close();


        BufferedReader in = new BufferedReader(new InputStreamReader(

                connection.getInputStream()));

        String decodedString;

        while ((decodedString = in.readLine()) != null) {

            System.out.println(decodedString);

        }

        in.close();

    }

}


展开阅读全文