论坛首页 编程语言技术论坛

Ruby二进制文件转码到Base64并通过Post进行提交

浏览 2704 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-02-08  
ruby -v 写道
ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
 

做这个小程序主要是将本地的数据库文件通过普通的Form表单提交到数据库进行存储。在这里不直接用file传输,主要是因为系统提供的服务不支持文件处理,那么就麻烦下客户端将数据转换成base64作为普通表项提交好了。

 

$KCODE = 'utf8'

require 'base64'
require 'net/http'
require 'uri'
require 'cgi'

data = File.read('default.db')

b64= Base64.encode64(data)

http = Net::HTTP.new('www.abc.com', 80)

path = '/path'

data = "data=#{CGI.escape(b64)}"
headers = {}

resp, data = http.post(path, data, headers)

puts 'Code = ' + resp.code
puts 'Message = ' + resp.message
resp.each {|key, val| puts key + ' = ' + val}

 

 带有SSL加密的请求

$KCODE = 'utf8'

require 'base64'
require 'net/http'
require 'net/https'
require 'uri'
require 'cgi'

data = File.read('default.db')

b64= Base64.encode64(data)

http = Net::HTTP.new('www.abc.com', 443)
http.use_ssl = true

#Turning off verify_mode for SSL
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

path = '/path'

# GET request -> so the host can set his cookies
#resp, data = http.get(path, nil)

data = "data=#{CGI.escape(b64)}"
headers = {
}

resp, data = http.post(path, data, headers)

puts 'Code = ' + resp.code
puts 'Message = ' + resp.message
resp.each {|key, val| puts key + ' = ' + val}
 
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics