<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fleurer &#187; http-basic</title>
	<atom:link href="http://www.fleurer-lee.com/tag/http-basic/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fleurer-lee.com</link>
	<description>rage and love, story of my life.</description>
	<lastBuildDate>Thu, 02 Sep 2010 11:07:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ruby使用open-uri做http basic验证</title>
		<link>http://www.fleurer-lee.com/2009/01/29/ruby%e4%bd%bf%e7%94%a8open-uri%e5%81%9ahttp-basic%e9%aa%8c%e8%af%81/</link>
		<comments>http://www.fleurer-lee.com/2009/01/29/ruby%e4%bd%bf%e7%94%a8open-uri%e5%81%9ahttp-basic%e9%aa%8c%e8%af%81/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 10:54:50 +0000</pubDate>
		<dc:creator>ssword</dc:creator>
				<category><![CDATA[备忘]]></category>
		<category><![CDATA[http-basic]]></category>
		<category><![CDATA[open-uri]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://ssword.yo2.cn/2009/01/29/ruby%e4%bd%bf%e7%94%a8open-uri%e5%81%9ahttp-basic%e9%aa%8c%e8%af%81/</guid>
		<description><![CDATA[如今的web api基本上都是用http basic作为验证方式, 在浏览器中的话只要将用户名和密码用和一个@放到url的前面即可,如http://ssword:password@somedomain.com , 使用curl也是简单无比, 如饭否api的示例, 它可以验证你的身份并获得你好友的信息, 若密码错误, 就得到一个401的错误
curl -u loginname:password http://api.fanfou.com/statuses/friends_timeline.rss
在ruby中的解决方法有很多, 只是我不喜欢封装的库函数, 记不住那东西. http basic验证只是http协议的一部分嘛, 简单地加个http头不就行了? 再使用open-uri的话就更简单不过了, 它允许你像打开本地文件那样打开uri的内容, 并可以添加http头, 十分方便, 如下:
require &#8216;open-uri&#8217;
require &#8216;base64&#8242;
f = open(url,&#8221;Authorization&#8221;=>&#8221;Basic #{Base64.b64encode(&#8220;myaccount:mypassword&#8221;)}&#8221;)
puts f.read
]]></description>
			<content:encoded><![CDATA[<p>如今的web api基本上都是用http basic作为验证方式, 在浏览器中的话只要将用户名和密码用和一个@放到url的前面即可,如http://ssword:password@somedomain.com , 使用curl也是简单无比, 如饭否api的示例, 它可以验证你的身份并获得你好友的信息, 若密码错误, 就得到一个401的错误</p>
<p>curl -u loginname:password http://api.fanfou.com/statuses/friends_timeline.rss</p>
<p>在ruby中的解决方法有很多, 只是我不喜欢封装的库函数, 记不住那东西. http basic验证只是http协议的一部分嘛, 简单地加个http头不就行了? 再使用open-uri的话就更简单不过了, 它允许你像打开本地文件那样打开uri的内容, 并可以添加http头, 十分方便, 如下:</p>
<p>require &#8216;open-uri&#8217;<br />
require &#8216;base64&#8242;<br />
f = open(url,&#8221;Authorization&#8221;=>&#8221;Basic #{Base64.b64encode(&#8220;myaccount:mypassword&#8221;)}&#8221;)<br />
puts f.read</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fleurer-lee.com/2009/01/29/ruby%e4%bd%bf%e7%94%a8open-uri%e5%81%9ahttp-basic%e9%aa%8c%e8%af%81/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
