4,459,880 th visitor since 2017.2.1 ( Today : 9459 )
Programming
No. 802
Name. swindler
Subject. wget 호출하여 페이지 가져오기
Main Cate. Java
Sub Cate.
Date. 2018-04-10 10:58
Hit. 1752 (59.29.233.1)
File.
wget을 호출하여 페이지를 가져올때
-O 옵션으로 특정파일에 쓰고, 다시 읽어들이는건 문제의 소지가 많다.

-O - 옵션을 주면, 가져온 페이지를 화면에 보여주므로 이걸 InputStream 으로 읽어들이면 된다.




대략 아래와 같은 형태
header 세팅도 당연히 가능하다.


    public static String wget(String url, String postdata) {

        String html = "";

        String command = null;
        Runtime rt = Runtime.getRuntime();
        Process p = null;
        int ret = 0;
        StringBuffer buf = new StringBuffer();

        try {

            if(postdata==null)
                command = "umask 0000;/usr/bin/wget -q --timeout=60 --tries=1 --no-check-certificate -O - '"+url+"'";
            else
                command = "umask 0000;/usr/bin/wget -q --timeout=60 --tries=1 --no-check-certificate --post-data='"+postdata+"' -O - '"+url+"'";

            // wget --header='token: 1234' --header='token2: 5678'

            p = rt.exec(new String [] {"bash", "-c", command} );

            String line = null;
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while((line = in.readLine())!=null) {

                buf.append(line);
            }

            p.destroy();

        }catch(Exception e) {
            
            System.out.println(e);
            e.printStackTrace();
        }

        return buf.toString();
    }

[바로가기 링크] : http://coolx.net/cboard/develop/802



Name
Password
Comment
Copyright © 1999-2017, swindler. All rights reserved. 367,611 visitor ( 1999.1.8-2004.5.26 ), 2,405,771 ( -2017.01.31)

  2HLAB   2HLAB_Blog   RedToolBox   Omil   Omil_Blog