Created
March 6, 2012 13:33
-
-
Save kano4/1986295 to your computer and use it in GitHub Desktop.
How to modify User-Agent in Mechanize and Net::HTTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Mechanize http://d.hatena.ne.jp/takuya_1st/20120301/1330605502 | |
mech = Mechanize.new | |
mech.user_agent_alias = 'Windows IE 7’ #キーを指定。 | |
AGENT_ALIASES = { | |
'Windows IE 6' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', | |
'Windows IE 7' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)', | |
'Windows Mozilla' => 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6', | |
'Mac Safari' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; de-at) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10', | |
'Mac FireFox' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6', | |
'Mac Mozilla' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.4a) Gecko/20030401', | |
'Linux Mozilla' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624', | |
'Linux Firefox' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.1) Gecko/20100122 firefox/3.6.1', | |
'Linux Konqueror' => 'Mozilla/5.0 (compatible; Konqueror/3; Linux)', | |
'iPhone' => 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C28 Safari/419.3', | |
'Mechanize' => "WWW-Mechanize/#{VERSION} (http://rubyforge.org/projects/mechanize/)" | |
} | |
# net/http http://blog.cles.jp/item/3367 | |
require 'net/http' | |
Net::HTTP.version_1_2 | |
USER_AGENT= "TestAgent" | |
Net::HTTP.start('127.0.0.1', 80) {|http| | |
response = http.get('/') | |
response = http.get('/', 'User-Agent' => USER_AGENT ) | |
} | |
> 127.0.0.1 - - [05/Jan/2010:21:26:31 +0900] "GET / HTTP/1.1" 200 2343 "-" "-" | |
> 127.0.0.1 - - [05/Jan/2010:21:26:31 +0900] "GET / HTTP/1.1" 200 2343 "-" "TestAgent" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment