查看: 6564|回复: 0
打印 上一主题 下一主题

linux下的回车

[复制链接]
跳转到指定楼层
1#
发表于 2008-7-29 17:17:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
今天,用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请求。
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表