在验证时机械化405错误

时间:2015-11-09 02:34:22

标签: ruby-on-rails ruby mechanize

我正在尝试使用Mechanize gem对网站进行身份验证,并收到以下错误:

405 => Net::HTTPMethodNotAllowed for https://www.mygreatlakes.org/educate/login/snapAuthentication -- unhandled response

表单使用POST作为方法,因此我不确定为什么会出现此错误。使用Hurl It,我能够看到此URL接受GET请求。访问此URL会返回401错误说明

Error 401: Authentication Failed: Authentication method not supported: GET

也许值得注意的是:有一个用户名输入和表单之外的另一个输入,我想知道他们在表单之外的位置是否意味着JavaScript被用于将这些添加到请求中?

这是我用来登录的代码:

  def scraper
    agent = Mechanize.new { |a| a.log = Logger.new("log/mechanize.log") }
    agent.request_headers = {'User-agent' => 'Mozilla/5.0 (Windows NT 5.2; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11'}

    login_page = agent.get('https://www.mygreatlakes.org/educate/login.html')
    login_form = login_page.form_with(id: 'snapLogin')
    username_field = login_form.field_with(name: "userName")
    username_field.value = "username"
    login_page = login_form.submit

    pin_form = login_page.form_with(id: 'securityPinModel')
    pin_field = pin_form.field_with(name: "pinNumber")
    pin_field.value = "pin"
    pin_page = pin_form.submit

    password_form = pin_page.form_with(id: "frm")
    password_field = password_form.field_with(name: "password")
    password_field.value = "password"
    password_page = password_form.submit

    @output = password_page.body
  end

这是来自密码页面上的表格中的HTML,它引发了错误:

<form id="frm" class="slow-load log-in" autocomplete="OFF" action="snapAuthentication" method="post">
                    <div id="errors" class="message alert hide-this authErrors" role="alert" style="display: none;">
                        <strong>Please Correct These Errors</strong><br>
                        <ul id="errorList" style="outline: none;" tabindex="-1">

                        </ul>
                    </div>      


<div>
    <p>Here's the identity image and catchphrase you selected:</p>
    <p class="display-img">
        <img style="height: 65px; width: 65px;" alt="Pirate Flag" src="image.img?id=7"> 
        <br>
        <strong class="blue">catchphrase</strong>
    </p>
</div>
                        <div>
                            <label for="password">Password</label> 
                            <input id="password" name="password" class="short input-field" type="password" value="" autocomplete="off">
                        </div>
                    <button type="submit" class="orange" id="authenticate">Continue</button>
                    <p class="help"><a class="nowrap" href="/educate/login/trouble.html">I forgot my password</a></p>
                </form>
<input id="userName" name="userName" type="hidden" value="username">
<input type="hidden" name="_csrf" value="value">

这是我的日志文件:

I, [2015-11-08T21:26:22.626822 #48752]  INFO -- : form encoding: utf-8
D, [2015-11-08T21:26:22.626999 #48752] DEBUG -- : query: "password=password"
I, [2015-11-08T21:26:22.629630 #48752]  INFO -- : Net::HTTP::Post: /educate/login/snapAuthentication
D, [2015-11-08T21:26:22.629804 #48752] DEBUG -- : request-header: accept-encoding => gzip,deflate,identity
D, [2015-11-08T21:26:22.629886 #48752] DEBUG -- : request-header: accept => */*
D, [2015-11-08T21:26:22.629956 #48752] DEBUG -- : request-header: user-agent => Mozilla/5.0 (Windows NT 5.2; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11
D, [2015-11-08T21:26:22.630022 #48752] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2015-11-08T21:26:22.630087 #48752] DEBUG -- : request-header: accept-language => en-us,en;q=0.5
D, [2015-11-08T21:26:22.630168 #48752] DEBUG -- : request-header: cookie => GLDCID=.mad; JSESSIONID_007_infoserv=0001LgdwpswRf5r66r4pAgOtaOM:153b6ufq4; TLTSID=0.5241251b52c6a
D, [2015-11-08T21:26:22.630235 #48752] DEBUG -- : request-header: host => www.mygreatlakes.org
D, [2015-11-08T21:26:22.630310 #48752] DEBUG -- : request-header: referer => https://www.mygreatlakes.org/educate/login/imageAndPassword.html
D, [2015-11-08T21:26:22.630377 #48752] DEBUG -- : request-header: content-type => application/x-www-form-urlencoded
D, [2015-11-08T21:26:22.630441 #48752] DEBUG -- : request-header: content-length => 17
I, [2015-11-08T21:26:22.680558 #48752]  INFO -- : status: Net::HTTPMethodNotAllowed 1.1 405 Method Not Allowed
D, [2015-11-08T21:26:22.680816 #48752] DEBUG -- : response-header: date => Mon, 09 Nov 2015 02:26:22 GMT
D, [2015-11-08T21:26:22.680936 #48752] DEBUG -- : response-header: server => Apache
D, [2015-11-08T21:26:22.681050 #48752] DEBUG -- : response-header: strict-transport-security => max-age=31536000 ; includeSubDomains
D, [2015-11-08T21:26:22.681164 #48752] DEBUG -- : response-header: x-xss-protection => 1; mode=block
D, [2015-11-08T21:26:22.681274 #48752] DEBUG -- : response-header: x-frame-options => DENY
D, [2015-11-08T21:26:22.681384 #48752] DEBUG -- : response-header: x-content-type-options => nosniff
D, [2015-11-08T21:26:22.681493 #48752] DEBUG -- : response-header: allow => GET,HEAD
D, [2015-11-08T21:26:22.681601 #48752] DEBUG -- : response-header: content-length => 0
D, [2015-11-08T21:26:22.681710 #48752] DEBUG -- : response-header: content-type => text/plain; charset=ISO-8859-1
D, [2015-11-08T21:26:22.681818 #48752] DEBUG -- : response-header: content-language => en-US
D, [2015-11-08T21:26:22.681926 #48752] DEBUG -- : response-header: keep-alive => timeout=4, max=95
D, [2015-11-08T21:26:22.682098 #48752] DEBUG -- : response-header: connection => Keep-Alive
D, [2015-11-08T21:26:22.682332 #48752] DEBUG -- : Read 0 bytes (0 total)

1 个答案:

答案 0 :(得分:-1)

该错误表示该网站正在阻止您尝试连接的方法。

网站的管理员基本上阻止了您尝试使用的特定方法,或者该网站处于“只读”状态,但您可以移除此块,这里有一个链接,教您如何操作:

http://www.checkupdown.com/status/E405.html

对于401错误,它可以指示旧的缓存数据,丢失的cookie或安全冲突。 401错误也可能表示服务器错误。