获取URL地址的返回值

URL url = null;
        try {
            url = new URL("http://a.test.com/test.asp?action=get");
        } catch (MalformedURLException ex1) {
        }
        HttpURLConnection connection = null;
        try {
            connection = (HttpURLConnection) url.openConnection();
        } catch (IOException ex2) {
            ex2.printStackTrace();
            return;
        }

        BufferedReader urlIn = null;
        try {
            urlIn = new BufferedReader(new InputStreamReader(connection.
                    getInputStream(), "gb2312"));
        } catch (IOException ex3) {
            ex3.printStackTrace();
            return;
        }
        String urlLine = "";
        StringBuffer line = new StringBuffer();
        try {

            while ((urlLine = urlIn.readLine()) != null) {
                line.append(urlLine.trim());
            }
        } catch (IOException ex4) {
            ex4.printStackTrace();
            return;
        }
        try {
            urlIn.close();
        } catch (IOException ex5) {
            ex5.printStackTrace();
            return;
        }
        urlLine = line.toString();

转载于:https://www.cnblogs.com/maikuraki/archive/2012/11/24/2785583.html

你可能感兴趣的:(获取URL地址的返回值)