Title / Description
Code require "socket"; require "uri"; # urlencoded type: application/x-www-form-urlencoded class WebClient attr_writer :action, :type; attr_reader :initial_line, :headers, :content; def initialize server, port = 80 @server, @port = server, port; @action = "GET"; @type = "text/plain"; end def request location, data=nil socket = TCPSocket.new @server, @port; if data != nil and @action == 'GET' @action = 'POST'; end socket.write "#{@action} #{URI.escape(location)} HTTP/1.0\r\n"; socket.write "Host: #{@server}\r\n"; if data != nil socket.write "Content-Type: #{@type}\r\n"; socket.write "Content-Length: #{data.length}\r\n"; end socket.write "\r\n"; socket.write data; @initial_line = socket.readline; @headers = {}; while (line = socket.readline) != "\r\n" @headers.store *line.downcase.strip.split(/:\s+/); end @content = socket.read; end end
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code