簡體   English   中英

使用Mechanize gem登錄

[英]Login using Mechanize gem

My code is this


 require 'mechanize'
  obj=Mechanize.new
  a.get("http://cafedepoca.com/submit_data?name=test&email=test@mail.com&pass=mypassword&prof_type=30&api_key=mykey") do |d|
  d.user='testuser'
  d.password='mypassword'
  end.click_button

它返回並返回一個error.Net::HTTPUnauthorized 。如果以其他方式在彈出框中填充憑據。如果存在其他任何寶石,則建議我。

我檢查了http://cafedepoca.com ,並提示使用BasicAuthentication。 您必須先輸入。

 require 'mechanize'
 a=Mechanize.new
 a.auth('username','password') # fix this line with correct login and password for BasicAuth  
 a.get("http://cafedepoca.com/submit_data?name=test&email=test@mail.com&pass=mypassword&prof_type=30&api_key=mykey").click_button

它應該以這種方式工作。

您需要使用Mechanize#authMechanize#add_auth來通過基本身份驗證。

require 'mechanize'

USERNAME = "XXXXXXXX"
PASSWORD = "YYYYYYYY"

agent = Mechanize.new
agent.auth(USERNAME, PASSWORD)
agent.get("http://cafedepoca.com/submit_data?name=test&email=test@mail.com&pass=mypassword&prof_type=30&api_key=mykey")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM