HTTP network library

Looking for a script that provides wrapper methods for connecting to some server and sending/receiving data via HTTP (and maybe other protocols, but they are not important). Other "useful" methods would be nice as well, whatever they might be (like handling different types of content?)


I am aware of a couple but I don't remember what they're called.


What is everyone else using?
Dr. Fomar has a script for HTTP connection.

I have seen a script for socket and a script for HTTP download too, but don't remember where I got them (socket was made by Zeus81 I think)

Socket:

Spoiler
#==============================================================================# ** Socket#------------------------------------------------------------------------------# Adds the possibility to send/receive messages to/from a server# Big thanks to Zeus81 (and to Nuki, too)#==============================================================================module Socket #-------------------------------------------------------------------------- # * Win32API #-------------------------------------------------------------------------- Socket = Win32API.new('ws2_32', 'socket', 'lll', 'l') Connect = Win32API.new('ws2_32', 'connect', 'ppl', 'l') Close = Win32API.new('ws2_32', 'closesocket', 'p', 'l') Send = Win32API.new('ws2_32', 'send', 'ppll', 'l') Recv = Win32API.new('ws2_32', 'recv', 'ppll', 'l') Inet_addr = Win32API.new('ws2_32', 'inet_addr', 'p', 'l') Htons = Win32API.new('ws2_32', 'htons', 'l', 'l') Shutdown = Win32API.new('ws2_32', 'shutdown', 'pl', 'l') #-------------------------------------------------------------------------- # * Constants #-------------------------------------------------------------------------- AF_INET = 2 SOCK_STREAM = 1 #-------------------------------------------------------------------------- # * Singleton #-------------------------------------------------------------------------- class << self #-------------------------------------------------------------------------- # * Creating the SOCKADDR_IN #-------------------------------------------------------------------------- def sockaddr_in(host, port) sin_family = AF_INET sin_port = Htons.(port) in_addr = Inet_addr.(host) sockaddr_in = [sin_family, sin_port, in_addr].pack('sSLx8') return sockaddr_in end #-------------------------------------------------------------------------- # * Creating the Socket #-------------------------------------------------------------------------- def socket() return Socket.(AF_INET, SOCK_STREAM, 0) end #-------------------------------------------------------------------------- # * Connecting #-------------------------------------------------------------------------- def connect_sock(sock, sockaddr) if Connect.(sock, sockaddr, sockaddr.size) == -1 p "Impossible to connect" return end p "Successfully connected" end #-------------------------------------------------------------------------- # * Connecting #-------------------------------------------------------------------------- def connect(host = "127.0.0.1", port = 9999) sockaddr = sockaddr_in(host, port) sock = socket() connect_sock(sock, sockaddr) return sock end #-------------------------------------------------------------------------- # * Sending data #-------------------------------------------------------------------------- def send(socket, data) value = Send.(socket, data, data.length, 0) if value == -1 p "Failed to send" shutdown(socket, 2) return false end p "Successfully sent" return true end #-------------------------------------------------------------------------- # * Receiving data #-------------------------------------------------------------------------- def recv(socket, len = 256) buffer = [].pack('x'+len.to_s) value = Recv.(socket, buffer, len, 0) return buffer.gsub(/\x00/, "") if value != -1 return false end #-------------------------------------------------------------------------- # * Stops the emission #-------------------------------------------------------------------------- def shutdown(socket, how) Shutdown.(socket, how) end #-------------------------------------------------------------------------- # * Closes the connection #-------------------------------------------------------------------------- def close(socket) Close.(socket) end end end
HTTP Download:

Spoiler
Code:
module Berka  module NetError    ErrConIn="Unable to connect to Internet"    ErrConHttp="Unable to connect to the Server"    ErrTranHttp="Http Download is failed"    ErrNoFile="No file to be download"    ErrMkdir="Unable to create a new directory"        HTTPSync=false #true: the game is faster but download are very slower:                   #false: 20secs & true: 1min  endendinclude Berka::NetErrormodule Net  W='wininet'  SPC=Win32API.new('kernel32','SetPriorityClass','pi','i').call(-1,128)  IOA=Win32API.new(W,'InternetOpenA','plppl','l').call('',0,'','',0)  IC=Win32API.new(W,'InternetConnectA','lplpplll','l')  print(ErrConIn)if IOA==0  module HTTP    IOU=Win32API.new(W,'InternetOpenUrl','lppllp','l')    IRF=Win32API.new(W,'InternetReadFile','lpip','l')    ICH=Win32API.new(W,'InternetCloseHandle','l','l')    HQI=Win32API.new(W,'HttpQueryInfo','llppp','i')    module_function    def sizeloaded(i='');@read[i];end    def transfered;@dloaded;end    def transfers;@dls;end    def progress(i='');(@read[i].to_f/@size[i]*100);end    def loaded?(i='');@read[i]>=@size[i]rescue nil;end    def temps(i='');@tps[i]if loaded?(i);end    def size(i='');@size[i];end    def download(url,int='./')      @dloaded||=0;@dls||={};@i||=-1;@size||={};@read||={};@tps={}      a=url.split('/');serv,root,fich=a[2],a[3..a.size].join('/'),a[-1]      print(ErrNoFile)if fich.nil?      @dls[fich]=Thread.start(url,int){|url,int|txt='';t=Time.now      ErrConHttp if(e=IC.call(IOA,serv,80,'','',3,1,0))==0        f=IOU.call(IOA,url,nil,0,0,0)      HQI.call(f,5,k="\0"*1024,[k.size-1].pack('l'),nil)      @read[fich],@size[fich]=0,k.delete!("\0").to_i      loop do        buf,n=' '*1024,0        r=IRF.call(f,buf,1024,o=[n].pack('i!'))        n=o.unpack('i!')[0]        break if r&&n==0        txt<<buf[0,n]        @read[fich]=txt.size        sleep(0.001)if HTTPSync      end      (File.open(int+fich,'wb')<<txt).close      @dloaded+=@read[fich]      ICH.call(f);sleep(0.01)      @tps[fich]=Time.now-t}    end  endend
LINK

That allows for both GET and POST http requests, also a demo on how to set it up 🙂

Edit:

Came across this post and figured i would add THIS

Original thread: https://forums.rpgmakerweb.com/threads/http-network-library.15009/