赞 | 3 |
VIP | 0 |
好人卡 | 39 |
积分 | 1 |
经验 | 101436 |
最后登录 | 2017-9-1 |
在线时间 | 2276 小时 |
Lv1.梦旅人 路人党员
- 梦石
- 0
- 星屑
- 52
- 在线时间
- 2276 小时
- 注册时间
- 2010-12-30
- 帖子
- 3225
|
kuerlulu 发表于 2015-2-24 15:04
发一个我刚写的Http#用之前先把上一个exe版本的Http删掉【本人的水平拙劣,代码风格奇特,恐不符大众口味, ...
帮你改进了一些==- class HTTP
- Create = Win32API.new('AHttp.dll', 'Create', 'pppp', 'l')
- CreateEx = Win32API.new('AHttp.dll', 'CreateEx', 'pppppp', 'l')
- Query = Win32API.new('AHttp.dll', 'QueryEx', 'lpp', 'i')
- Retrieve = Win32API.new('AHttp.dll', 'Retrieve', 'lp', 'i')
- Close = Win32API.new('AHttp.dll', 'Close', 'l', 'l')
- INVALID = 0; SUCCESS = 1; FAILURE = 2; PENDING = 3
- def initialize(type, server, action, meth, data, ctype = "", browser = "")
- @token = type == 0 ?
- Create.call(server, action, meth, data) :
- CreateEx.call(server, action, meth, ctype, browser)
- @total = [0].pack("L")
- @loaded = [0].pack("L")
- end
- def total_bytes
- query
- return @total.unpack("L").first
- end
- def loaded_bytes
- query
- return @loaded.unpack("L").first
- end
- def query
- Query.call(@token, @total, @loaded)
- end
- def retrieve
- if query == SUCCESS
- length = Retrieve.call(@token, 0)
- buffer = "\0" * length
- Retrieve.call(@token, buffer)
- return buffer
- end
- end
- def close
- Close.call(@token)
- end
- def self.create(server, action, meth, data={})
- @conn = new(0, server, action, meth, data_encode(data))
- if block_given?
- yield @conn
- @conn.close
- else
- return @conn
- end
- end
- def self.create_ex(server, action, meth, ctype, browser, data={})
- @conn = new(0, server, action, meth, data_encode(data), ctype, browser)
- if block_given?
- yield @conn
- @conn.close
- else
- return @conn
- end
- end
- def self.data_encode(data)
- d = ""
- data.each_pair do |k, v|
- d += "%" + k.to_s.unpack("C*").map{|s|s.to_s(16)}.join("%") + "="
- d += "%" + v.to_s.unpack("C*").map{|s|s.to_s(16)}.join("%") + "&"
- end
- return d.sub(/.$/, "")
- end
- private_class_method :new, :data_encode
- end
复制代码
- s = Sprite.new
- b = Bitmap.new(640, 32)
- s.bitmap = b
- #HTTP.create("localhost", "projects/test.php", "POST", {"test"=>"hahahaha"}){ |h|
- HTTP.create("rpg.blue", "forum.php", "GET"){ |h|
- while(!h.retrieve)
- b.clear
- b.draw_text(0,0, 640, 32, "#{h.query}:#{h.loaded_bytes}/#{h.total_bytes}")
- Graphics.update
- end
- b.clear
- b.draw_text(0,0, 640, 32, "#{h.query}:#{h.loaded_bytes}/#{h.total_bytes}")
- Graphics.update
- print(h.retrieve)
- }
- exit
复制代码 |
评分
-
查看全部评分
|