一.mechanize 代码
require "mechanize"
agent = Mechanize.new
login_page = agent.get "https://ruby-china.org/account/sign_in"
login_form = login_page.forms[1]
username_field = login_form.field_with(:name => "user[login]")
username_field.value = "[email protected]"
password_field = login_form.field_with(:name => "user[password]")
password_field.value = "xxx"
step2_page = agent.submit login_form
tmp_cookie = agent.cookie_jar
agent.cookie_jar = tmp_cookie
index_page = agent.get("https://ruby-china.org/")
re = /^flowerwrong/
puts index_page.body.to_s[0..5000]
请问使用 curl 怎么模拟登陆,我使用的是 curb 库,麻烦看那下问题所在,谢谢。
require 'curb'
USER_NAME = "[email protected]"
PASS = "xxx"
user = {"user\[login\]" => USER_NAME, "user\[password\]" => PASS}
login_url = "https://ruby-china.org/account/sign_in"
c = Curl::Easy.new
c.url = login_url
c.enable_cookies = true
c.cookiejar = "cookies.txt"
c.http_post(c.url, user)
puts c.header_str
http header 打印结果
flowerwrong@flowerwrong:~/dev/ruby/mechanize$ ruby ruby_china.rb
HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Tue, 19 Aug 2014 09:13:31 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Strict-Transport-Security: max-age=31536000
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
ETag: "51a9034a6f409ef3e8944e80113415bb"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 4f6f03ef-b598-43dd-9fc2-49d388bb3d5c
X-Runtime: 0.039903