0

使用 Ruby,我只是尝试将比特流文件解析到服务器,我遇到了 badrequst HTTP 的问题。任何人都可以帮助我使用 Net::HTTP 将数据发送到服务器。

  def addbitstream(url, path, file_bitstream)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, 443)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Post.new(path)

  f = File.new(file_bitstream)
  file = File.open(f)
  n = 6
  offset = 0
  request.body = ""

  while (offset < File.size(file))
    buffer = readfileAsbitstream(file, offset, n)
    request.body = buffer
    response = Net::HTTP.start(uri.host, 443) {|http| http.request(request) }

    offset += n
  end
end
4

2 回答 2

0

Although I have not done file streaming, the first problem you will have with this code is that HTTP.start closes the connection after executing a block, when it is passed one. Maybe changing the order of your nesting here will help.

I would recommend using a gem to wrap HTTP requests anyway such as REST-client, which I think streams file uploads by default.

于 2013-04-11T15:05:03.187 回答
0

以下是使用方法 Net::HTTP

url ="www.yoururl.com"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
http.use_ssl = true 
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.request(request)
于 2013-04-11T15:45:35.467 回答