前文http://blog.csdn.net/sheismylife/article/details/9237925 演示了如何手动的通过运行curl命令登录HTTPS站点,然后获取cookie, 再拼凑一个命令加上cookie访问网站其他的页面。通过newlisp编程可以将第一个命令输出的结果用正则表达式过滤出其中的cookie,然后发出请求。
下面是代码:
#!/usr/bin/newlisp (define (login email pwd) (set 'login-cmd (append "curl -v --insecure -d "email=" email "&pwd=" pwd "&language=en" " "https://localhost/your_site/login 2>&1")) (exec login-cmd)) (define (post cookie url) (set 'post-cmd (append "curl -v --cookie "" cookie "" " url " 2>&1")) (println post-cmd) (exec post-cmd)) (set 'r (login "gche@youmail.com" "123456")) (set 's (length r)) (set 'i 0) (set 'x nil) (do-until x (set 'x (regex "cml_session=[^;]+" (r i))) (inc i) ) (set 'cookie (first x)) (set 'r (post cookie "http://localhost/your_site/home")) (println r) (exit)
2>&1 是必须的,否则拿不到curl命令返回的头部信息。