我在受基本身份验证保护的外部服务(someurl.com/xmlimport.html)上获得了以下表格。
<html>
<head><title>Upload</title></head>
<body>
<h1>Upload</h1>
<h2>XML Upload</h2>
<!--<form action="/cgi-bin/xmlimport.pl" method="post" accept-charset="UTF-8" enctype="multipart/form-data">-->
<form action="/cgi-bin/xmlimport.pl" method="post" enctype="multipart/form-data">
Datei: <input name="dateiname" type="file" size="100" accept="text/*">
<input type="submit" value="Absenden"/>
</form>
</body>
</html>
我想通过 ruby 发布 xml 文件。这是我到目前为止得到的:
require "net/http"
require "uri"
uri = URI.parse('http://someurl.com/xmlimport.html')
file = "upload.xml"
post_body = []
post_body << "Content-Disposition: form-data; name='datafile'; filename='#{File.basename(file)}'rn"
post_body << "Content-Type: text/plainrn"
post_body << "rn"
post_body << File.read(file)
puts post_body
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.basic_auth "user", "pass"
request.body = post_body.join
request["Content-Type"] = "multipart/form-data"
resp = http.request(request)
puts resp.body
响应是我的 xml 文件和表单的内容。但没有任何处理。我究竟做错了什么?
提前致谢。