[Ruby] open-uri で https と basic 認証を越えてページを取得する

nokogiri でパースする前処理。
ローカルのファイルも扱える形で動かす。

[markdown]

“`ruby
#!/usr/bin/ruby
require ‘open-uri’
require ‘openssl’
# Basic auth
USER = ‘foo’
PASSWORD = ‘bar’
uri = ‘https://test.example.com/’
begin
html = open(uri,
{:http_basic_authentication => [USER, PASSWORD],
:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE}).read
rescue => e
puts e
end
puts html
“`

参考資料。

> * [Rubyist Magazine – 標準添付ライブラリ紹介 【第 7 回】 net/http](http://magazine.rubyist.net/?0013-BundledLibraries)
> * [library open-uri](http://docs.ruby-lang.org/ja/2.2.0/library/open=2duri.html)
> * [library net/http](http://docs.ruby-lang.org/ja/2.2.0/library/net=2fhttp.html)

## HTTP header を確認

Cocoa Packet Analyzer で Authorization を検索して確認もできる。

2015-04-09_open-uri_01

> * [Cocoa Packet Analyzer と Wireshark (homebrew/QT) をインストールする | deadwood](https://www.d-wood.com/blog/2014/03/04_5764.html)
> * [HTTP クライアントを作ってみよう(5) – Basic 認証編 -](http://x68000.q-e-d.net/~68user/net/http-auth-1.html)

## 補遺

> * [How to do basic authentication over HTTPs in Ruby? – Stack Overflow](http://stackoverflow.com/questions/13822555/how-to-do-basic-authentication-over-https-in-ruby)
> * [Rubyでnet/http(Net::HTTP)を使う駄目コードを書いてたので直した | EasyRamble](http://easyramble.com/fix-ruby-net-http-bad-code.html)
> * [rubyで通信系の関数(Net関数)を使うときに気をつけること – Qiita](http://qiita.com/reikubonaga/items/4bef1e107e6067dd6bf6)
[/markdown]