1 public static String regular(String possess, String fill){ 2 //正则表达式 3 String csrf_token = null; 4 BufferedReader in; 5 Pattern pattern = Pattern.compile(possess); 6 in = new BufferedReader(new StringReader(fill)); 7 String s; 8 try { 9 while ((s = in.readLine()) != null){ 10 Matcher matcher = pattern.matcher(s); 11 if (matcher.find()) 12 { 13 csrf_token=matcher.group(1); 14 } 15 } 16 } catch (IOException e) { 17 e.printStackTrace(); 18 } 19 try { 20 in.close(); 21 } catch (IOException e) { 22 e.printStackTrace(); 23 } 24 return csrf_token; 25 }
1 //获取csrf_token值(使用正则) 2 csrf_token = LunTan.regular("name="csrf_token" value="(.+?)"/>", response); 3 System.out.println("csrf_token:" + csrf_token); 4 System.out.println("----------------------------------------");