1. 程式人生 > >curl java 模擬http請求

curl java 模擬http請求

col ont font nbsp pri tin throw url while

curl java 模擬http請求

直接上代碼:

 1 public static void main(String args[]) throws Exception {
 2 
 3         String url = "https://www.baidu.com";
 4         String[] cmds = {"curl",url};
 5         ProcessBuilder pb = new ProcessBuilder(cmds);
 6         pb.redirectErrorStream(true);
 7         Process p;
8 try { 9 p = pb.start(); 10 BufferedReader br = null; 11 String line = null; 12 br = new BufferedReader(new InputStreamReader(p.getInputStream())); 13 while ((line = br.readLine()) != null) { 14 System.out.println("\t" + line);
15 } 16 br.close(); 17 } catch (IOException e) { 18 e.printStackTrace(); 19 } 20 21 }

curl java 模擬http請求