<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/platform.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d4684235500622716427\x26blogName\x3dCaiwangqin\x27s+blog\x26publishMode\x3dPUBLISH_MODE_HOSTED\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttp://blog.caiwangqin.com/search\x26blogLocale\x3dzh_CN\x26v\x3d2\x26homepageUrl\x3dhttp://blog.caiwangqin.com/\x26vt\x3d3393395200455623441', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe", messageHandlersFilter: gapi.iframes.CROSS_ORIGIN_IFRAMES_FILTER, messageHandlers: { 'blogger-ping': function() {} } }); } }); </script>

Caiwangqin's blog

Focus on Life, Cloud Service, Smart Hardware, Architecture, Technic and beyond…

Ruby 代码-服务器端获取HTTP网站内容

2006年9月20日星期三

使用Ruby的Network and Web Libraries,能够非常方便的获取一个HTTP网页的内容,如获取优友地带首页的内容:


require ‘net/http’


h = Net::HTTP.new(’www.uuzone.com’, 80)

resp, data = h.get(’/html/friend/’, nil )

puts “Code = #{resp.code}”

puts “Message = #{resp.message}”

resp.each {|key, val| printf “%-14s = %-40.40s\n”, key, val }

p data[0..55]


以上代码片断中的data即为返回的网页内容,关于Ruby 网络应用的详细参考可见《Programming Ruby》中的Network and Web Libraries章节,如果你是在Windows下安装Ruby, 这本书就在Ruby安装目录下:doc/ProgrammingRuby.chm


ZhouRuby扩展还为Ruby扩展了一个HttpRequest类,将Ruby对网络的支持又变得简单易用了很多,非常好用。


标签:

posted by Caiwangqin, 01:33

0 Comments:

发表评论

订阅 博文评论 [Atom]

<< 主页