From: rubikitch Date: 2000-02-17T14:21:57+09:00 Subject: [ruby-list:20791] Re: Where is http-access.rb? るびきちです。 うーん、 http-access.rb では POST 使えないんか。 http-access.rb を知る前だけど一応自前で class HTTP を書いてみた。 主に掲示板カキコが目的。 w3m に依存している。 こんな感じで使える。 hash で与えられるところが我ながら気に入っている。 HTTP::post (url, { 'handle' => name, 'email' => email, 'subject' => subject, 'url' => page, 'message' => body, 'key' => 'abcdef', 'etc1' => '', 'etc2' => '', 'etc3' => '', 'etc4' => '', 'etc5' => '', }.update(opt), ref).display_html require 'netinit' require 'cgi' require 'kconv' require 'socket' class HTTP include NetConstant def HTTP::display_html(url, cols=80) system ("w3m -dump -e -cols #{cols} #{url}") end def HTTP::hash2data(hash) data = '' hash.each_pair do |name, value| data << "&#{name}=#{CGI::escape(Kconv::tosjis value)}" if value end data[1 .. -1] end def initialize (url) @url = url @html = '' end attr :html attr :url def HTTP::post(url, data, referer=url, use_proxy=true) http = HTTP::new url http.post_data(data, referer, use_proxy) http end def post_data(data=@data, referer=url, use_proxy=true) proxy = TCPsocket::open(PROXY_SERVER, PROXY_PORT) data = case data when String data when Hash HTTP::hash2data data end proxy.print "POST #{url} HTTP/1.0", CRLF proxy.print "Accept: */*", CRLF # proxy.print "Content-type: application/x-www-form-urlencoded", CRLF proxy.print "Content-Length: #{data.size}", CRLF proxy.print "Referer: #{referer}", CRLF proxy.print "User-Agent: Ruby-chan", CRLF,CRLF proxy.print data.chomp, CRLF proxy.flush html=proxy.readlines.join proxy.close @html = html end def display_html(cols=80) w3m = IO.popen ("w3m -dump -e -T text/html -cols #{cols}", "w") w3m.print html w3m.close end end るびきち☆