2008-07-25
linux下的回车
今天,用ruby写一段执行文件上传的post代码的时候,我写下如下代码:
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请求
参考:
http://www.w3.org/TR/html401/interact/forms.html#h-17.10
http://www.faqs.org/rfcs/rfc2388.html
http://www.faqs.org/rfcs/rfc1867.html
http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
2008.7.25 10:46 星期五
发表评论
- 浏览: 3596 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
我的相册
theme_dir.PNG
共 14 张
共 14 张
最近加入圈子
链接
最新评论
-
Ruby中&&操作符的妙用(旁 ...
simulate if/elsif or case/when, javascri ...
-- by liusong1111 -
为Rails中的validation e ...
我的目的是将error信息按照某种xml结构形式返回给客户端,而不仅仅是通过&l ...
-- by woody_420420 -
为Rails中的validation e ...
1. article.errors.add( "api", "Error Mes ...
-- by hozaka -
Ruby On Rails-2.0.2源代 ...
强烈支持,又不是搞学术。简单易懂就好
-- by hykouyuu -
为Rails中的validation e ...
1.比如添加 errors 到 “api” 属性 ...
-- by woody_420420






评论排行榜