只有http头通信?- 请解释

Rad*_*dek 3 http-headers

下面的屏幕截图是使用 firebug 制作的。单击登录按钮后,捕获的 firefox <-> Web 服务器通信发生。但是我在我的 Firefox 中看不到任何重定向。这是否意味着 Firefox 和 Web 服务器仅在 http 标头级别进行通信?

我在 ruby​​/mechanize 中准备了脚本来从论坛中抓取一些细节。我使用vBulletin.org论坛对其进行了测试/编码。然后我设法登录到我的测试论坛并愉快地使用正确的论坛来完成我的脚本。但我无法登录。我的脚本仅返回我认为“未成功登录尝试”的登录页面。

  • 有人可能会向我解释下面的屏幕截图吗?

    • 特别是为什么有11行?为什么不只有一个?
    • 如果我只能理解blindly/without thinking对 Web 服务器的回复。我对吗?
  • 当我单击登录按钮时,firefox 会在帖子中发送,auth_username=myusername&auth_password=mypassword&auth_login=Login但我的脚本发送auth_username=radek&auth_password=mypassword是可以的,还是&auth_login=Login必须显示该部分?

  • 有没有机会在红宝石中模拟这种通信?

  • 或者我在想是否可以record进行交流,然后replay在我的脚本中(如果可能)

通讯截图

登录页面的html

<form class="login" method="post"> <fieldset>
<legend>Members Login</legend> 

<div>
<label for="auth_username">Username</label> <input id="auth_username" name="auth_username">
</div>

<div>
<label for="auth_password">Password</label> <input id="auth_password" name="auth_password" type="password">
</div>

</fieldset>
<div class="buttons">
<input name="auth_login" type="submit" value="Login"><p class="note"><a href="/forgotpassword">Forgot your password?</a></p>

</div>

</form>
Run Code Online (Sandbox Code Playgroud)

我的红宝石脚本

require 'rubygems'
require 'mechanize'
#agent = WWW::Mechanize.new

agent = WWW::Mechanize.new 

page = agent.get("http://www.vbulletin.org/forum/index.php")

login_form = page.form_with(:action => 'login.php?do=login')


puts 
login_form.fields.each { |f| puts "#{f.name} : #{f.value}" }    
login_form['vb_login_username'] = 'user name'
login_form['vb_login_password'] = ''

page = agent.submit login_form

output = File.open("login.html", "w") {|f| f.write(page.parser.to_html) }
Run Code Online (Sandbox Code Playgroud)

从不起作用的登录尝试中机械化日志

 INFO -- : Net::HTTP::Get: /login?auth_successurl=http://www.somedomain.com/forum/yota?baz_r=1
DEBUG -- : request-header: accept-language => en-us,en;q=0.5
DEBUG -- : request-header: connection => keep-alive
DEBUG -- : request-header: accept => */*
DEBUG -- : request-header: accept-encoding => gzip,identity
DEBUG -- : request-header: user-agent => WWW-Mechanize/0.9.3 (http://rubyforge.org/projects/mechanize/)
DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
DEBUG -- : request-header: host => www.somedomain.com
DEBUG -- : request-header: keep-alive => 300
DEBUG -- : Read 400 bytes
DEBUG -- : Read 1424 bytes
DEBUG -- : Read 2448 bytes
DEBUG -- : Read 3211 bytes
DEBUG -- : response-header: vary => Accept-Encoding
DEBUG -- : response-header: cache-control => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
DEBUG -- : response-header: connection => close
DEBUG -- : response-header: expires => Thu, 19 Nov 1981 08:52:00 GMT
DEBUG -- : response-header: content-type => text/html; charset=utf-8
DEBUG -- : response-header: date => Fri, 29 Jan 2010 23:43:12 GMT
DEBUG -- : response-header: content-encoding => gzip
DEBUG -- : response-header: server => Apache/2.2.3 (CentOS)
DEBUG -- : response-header: content-length => 3211
DEBUG -- : response-header: set-cookie => PHPSESSID=7cfilg86ju2ldcgso22246hpu4; path=/, WebStats:visitorId=lSMkcwuSWEE%3D; expires=Mon, 27-Jan-2020 23:43:12 GMT; path=/, WebStats:sessionId=%2B2HHK296t%2BQ%3D; expires=Mon, 27-Jan-2020 23:43:12 GMT; path=/
DEBUG -- : response-header: accept-ranges => bytes
DEBUG -- : response-header: pragma => no-cache
DEBUG -- : gunzip body
DEBUG -- : saved cookie: PHPSESSID=7cfilg86ju2ldcgso22246hpu4
DEBUG -- : saved cookie: WebStats:visitorId=lSMkcwuSWEE%3D
DEBUG -- : saved cookie: WebStats:sessionId=%2B2HHK296t%2BQ%3D
 INFO -- : status: 200
DEBUG -- : query: "auth_username=radek&auth_password=mypassword"
 INFO -- : Net::HTTP::Post: /login?auth_successurl=http://www.somedomain.com/forum/yota?baz_r=1
DEBUG -- : request-header: accept-language => en-us,en;q=0.5
DEBUG -- : request-header: connection => keep-alive
DEBUG -- : request-header: accept => */*
DEBUG -- : request-header: accept-encoding => gzip,identity
DEBUG -- : request-header: content-type => application/x-www-form-urlencoded
DEBUG -- : request-header: user-agent => WWW-Mechanize/0.9.3 (http://rubyforge.org/projects/mechanize/)
DEBUG -- : request-header: cookie => WebStats:sessionId=%2B2HHK296t%2BQ%3D; WebStats:visitorId=lSMkcwuSWEE%3D; PHPSESSID=7cfilg86ju2ldcgso22246hpu4
DEBUG -- : request-header: referer => http://www.somedomain.com/login?auth_successurl=http://www.somedomain.com/forum/yota?baz_r=1
DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
DEBUG -- : request-header: content-length => 43
DEBUG -- : request-header: host => www.somedomain.com
DEBUG -- : request-header: keep-alive => 300
DEBUG -- : Read 650 bytes
DEBUG -- : Read 1674 bytes
DEBUG -- : Read 2698 bytes
DEBUG -- : Read 3211 bytes
DEBUG -- : response-header: vary => Accept-Encoding
DEBUG -- : response-header: cache-control => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
DEBUG -- : response-header: connection => close
DEBUG -- : response-header: expires => Thu, 19 Nov 1981 08:52:00 GMT
DEBUG -- : response-header: content-type => text/html; charset=utf-8
DEBUG -- : response-header: date => Fri, 29 Jan 2010 23:43:13 GMT
DEBUG -- : response-header: content-encoding => gzip
DEBUG -- : response-header: server => Apache/2.2.3 (CentOS)
DEBUG -- : response-header: content-length => 3211
DEBUG -- : response-header: accept-ranges => bytes
DEBUG -- : response-header: pragma => no-cache
DEBUG -- : gunzip body
 INFO -- : status: 200
Run Code Online (Sandbox Code Playgroud)

Chr*_*Rea 6

重定向通常通过 HTTP 响应状态代码 301(永久移动)或 302(发现/临时移动)实现。 用于执行重定向的 301 和 302 HTTP 响应状态代码包含在 HTTP 标头中。

仅仅因为您没有“在 Firefox 中看到任何重定向”并不意味着没有发生重定向:它是在幕后进行的。

还有一种重定向方式,即在页面中使用 JavaScript。使用 JavaScript,重定向可以让用户一目了然,在那里他们可以首先显示一个页面,然后被重定向(可能在延迟之后)到另一个位置。但是,这些不是通过 HTTP 标头响应状态代码传输的 HTTP 重定向。

要解决您关于 Ruby 的问题:您应该在 Stack Overflow 上提出这个问题。但是,简而言之,是的,您应该能够使用任何服务器端 Web 技术设置 301 和 302 重定向响应代码。


更新:要解决以下评论中的进一步问题:

如果您想了解有关 HTTP 工作原理的更多信息,那么我推荐以下书: HTTP:David Gourley 和 Brian Totty 的权威指南。您还可以参考RFC 2616定义的官方 HTTP 标准:超文本传输​​协议 - HTTP/1.1

至于为什么您问题中的上述一系列请求需要 11 个步骤,您需要询问相关软件的原始作者。它可能可以用更少的步骤来实现,但可能不仅仅是 1 步。考虑一下,服务器需要至少一个重定向来在用户浏览器中设置 cookie,然后再将它们发送到受保护的资源。

由于您正在尝试复制基于 cookie 的自定义身份验证机制,因此我还建议您专注于研究此类系统通常是如何构建的。

如果您正在尝试编写登录页面的脚本,您应该确保您的程序向服务器发送的内容在各个方面都与真实请求相匹配,包括您当前未包含的参数。

此外,您需要确保您的代码接受并发服务器发送的任何 cookie。 不保留服务器发送的 cookie 是此类练习的常见错误。此外,您需要指示用于 HTTP 通信的任何库遵循重定向。其中一些默认情况下不遵循重定向。

最后 - 为了调试,从网页和您的程序中捕获请求和响应标头和正文,并比较它们。怀疑任何差异。