先看 ruby 代码
require "net/http"
require "open-uri"
require "uri"
@agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31' data = {"m"=>"user", "a"= >"login", "release"=>"20110501", "appid"=>"3"};
uri = URI.parse("http://localhost:8080/server/uc.php") request = Net::HTTP.post_form(uri, data)
request.initialize_http_header({"USER-AGENT" => "asdsdsd"}) puts request.class puts request.body
服务器端: <?php header("Content-Type: text/html; charset=utf-8"); echo $_SERVER['HTTP_USER_AGENT']."\n"; ?>
返回的结果是: “Ruby”而不是 @agent的内容 难道是 initialize_http_header 方法的问题 求帮助?