使用ruby Net:HTTP API将文件上载到远程apache服务器失败,发生409冲突

下面是一个将文件从本地文件系统上传到远程Apache服务器的程序。

该程序以409冲突错误结束。 有什么建议我做错了吗? 我在httpd.conf中启用了DAV并提供了所有必要的权限,但我仍然没有运气。 如果需要,我可以发布httpd.conf。

这是代码:

BOUNDARY = "AaB03xZZZZZZ11322321111XSDW" uri = URI.parse("http://localhost/dropbox/") file = "/tmp/KEYS.txt" http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Put.new(uri.request_uri) request.body_stream=File.open(file) request["Content-Type"] = "multipart/form-data" request.add_field('Content-Length', File.size(file)) request.add_field('session', BOUNDARY) response=http.request(request) puts "Request Headers: #{request.to_hash.inspect}" puts "Sending PUT #{uri.request_uri} to #{uri.host}:#{uri.port}" puts "Response #{response.code} #{response.message}" puts "#{response.body}" puts "Headers: #{response.to_hash.inspect}" 

它的输出:

 Request Headers: {"accept"=>["*/*"], "host"=>["localhost"], "content-length"=>[88873], "content-type"=>["multipart/form-data"], "session"=>["AaB03xZZZZZZ11322321111XSDW"], "connection"=>["close"]} Sending PUT /dropbox/ to localhost:80 Response 409 Conflict   409 Conflict  

Conflict

Cannot PUT to a collection.


Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.10 with Suhosin-Patch Server at localhost Port 80
Headers: {"server"=>["Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.10 with Suhosin-Patch"], "content-length"=>["315"], "content-type"=>["text/html; charset=ISO-8859-1"], "date"=>["Thu, 23 Aug 2012 17:36:40 GMT"], "connection"=>["close"]}

当我将第5行更改为此时,问题得以解决:

 request = Net::HTTP::Put.new("#{uri.request_uri}/test.file") 

错误“无法PUT到集合”意味着无法对文件夹进行上载。 必须指定文件名。

Interesting Posts