Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

60 lines (44 sloc) 1.57 kB
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href=./style.css>
</head>
<body>
<p>让Net::HTTP自动使用代理 [复制链接]</p>
<p>坛主</p>
<p>多币873 威望825 UID106537
串个门加好友打招呼发消息<br>
电梯直达1#
车迷 发表于 2011-11-4 15:46:47 |只看该作者 |倒序浏览
拜和谐的大防火墙所赐,调用微薄,人人等国外克隆网站的api需要用代理,而老外写的第3方lib通常没有考虑代理支持。 </p>
<p>一个小技巧,只要这些lib是用Net::HTTP来访问的,那么只要用一段小代码就能自动切换到Net::HTTP:roxy,而无需改动原始代码: </p>
<p>Ruby代码 </p>
<h1>这里从环境变量读取,可以改成从配置文件读取</h1>
<p>if ENV[&#39;http<em>proxy&#39;]<br>
module Net<br>
class &lt;&lt; HTTP<br>
alias ld</em>new :new </p>
<pre><code> def new(address, port = nil)
proxy = URI.parse(ENV[&#39;http_proxy&#39;])
Net::HTTP:roxy(proxy.host, proxy.port, proxy.user, proxy.password).old_new(address, port)
end
end
</code></pre>
<p>end<br>
end </p>
<h1>这里从环境变量读取,可以改成从配置文件读取</h1>
<p>if ENV[&#39;http<em>proxy&#39;]
module Net
class &lt;&lt; HTTP
alias ld</em>new :new</p>
<pre><code> def new(address, port = nil)
proxy = URI.parse(ENV[&#39;http_proxy&#39;])
Net::HTTP:roxy(proxy.host, proxy.port, proxy.user, proxy.password).old_new(address, port)
end
end
</code></pre>
<p>end
end</p>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.