wget 호출하여 페이지 가져오기 (Java)
2018-04-10 10:58 - swindler
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();
}
-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();
}