再舉一個完整一點的 http “GET” client 程式… 其實 http protocol 還設計的夠簡單又有彈性, 但有人可以教我如何讓它支援 http proxy 嗎? 實在是沒時間去研究…
# socket example - httpget
# usage: ruby httpget.rb http://host/path
require 'socket'
include Socket::Constants
host = "www.ntu.edu.tw" # default values
port = 80
path = "/"
# process URL (%r!..! means regular expression)
if %r!http://(.*?)(?::(\d+))?(/.*)! =~ ARGV[0]
host = $1
port = Integer($2) if $2
path = $3
end
#TCPSocket version
#socket = TCPsocket::open(host, port)
#send out HTTP header
socket.write("GET #{path} HTTP/1.0\r\n\r\n")
#
print socket.read
老闆總是覺得我們的系統跟 windows 一樣甚麼都有, 都可以寫的出來… 也不想想花多少錢在開發 kit 上, 那 kit 當然是夠陽春! 有時是感覺年紀越大越不敢說不! 學校也真不知在教甚麼? 難道沒學過就不會去找資料把它寫出來, 只會跟老闆說不會, 只好累到自己…
你可以參考 Ruby 的 socket 網路程式 1