引用:require 'net/http'
require 'uri'
url = URI.parse('http://myserver/services/upload.xml')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'username', 'password'
req['content-type'] = 'multipart/form-data;boundary=bbb'
......
req.body = %Q{--bbb
Content-Disposition: form-data; name="upload_file[]"; filename="my_file"
Content-Type: application/octet-stream
this is the content of the upload file.
this is the content of the upload file.
this is the content of the upload file.
--bbb
Content-Disposition: form-data; name="upload_file[]"; filename=""
Content-Type: application/octet-stream
--bbb--}
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
p res
不过这段代码始终执行错误,rails提示bad content之类。但是,我将header,body与w3g上的http关于multiple form的协议进行了比较,但是没发现有哪里不对。折腾了半天,只能调试跟踪到rails内部,发现rails在执行read_multipart方法,按照boundary截取request体的时候老是多截取一个字符。比如,对于body来说,我这里body的开始是--bbb,然后换行。但是在rails内部总是截取出--bbb C,多截取了一个C。纳闷了半天,突然想起,unix/linux下面的换行是:换行符(LF),不像windows下面换行是:回车(CR)换行(LF),但是http协议正好是按照boundary+CRLF进行request的解析,所以,造成我的request始终解析错误。所以,我将上面代码,关于body的赋值改为如下:
引用:req.body = %Q{--bbb\015
Content-Disposition: form-data; name="upload_file[]"; filename="my_file"
Content-Type: application/octet-stream\015\012\015
this is the content of the upload file.
this is the content of the upload file.
this is the content of the upload file.
--bbb\015
Content-Disposition: form-data; name="upload_file[]"; filename=""
Content-Type: application/octet-stream\015\012\015
--bbb--}
OK!大功告成,可以通过脚本成功的发送文件上传post请求。