HTTParty read_nonblock连接由对等Errno :: ECONNRESET重置

时间:2016-06-18 14:47:40

标签: ruby-on-rails ruby httparty

我是一名新的Ruby开发人员。我想同时发送一些并发请求(最多3个请求)来获取数据。我知道Ruby中有很多支持发送请求的Http库。最后,我选择了HTTParty,但是当我尝试发送请求时出现了一些错误。

response_data = Http::SearchFlightService.search(url, options)

网址值:

http://booknow.jetstar.com/Search.aspx?culture=vi-VN

选项的价值参数:

{:body=>{"search-origin01"=>"", "search-destination01"=>"", "ControlGroupSearchView$ButtonSubmit"=>"", "__VIEWSTATE"=>"", "undefined"=>"", "children"=>"0", "infants"=>"0", "ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_INFANT"=>"0", "ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_CHD"=>"0", "datedepart-01"=>"19/06/2016", "datereturn-01"=>"20/06/2016", "adults"=>"1", "ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketDay1"=>"19", "ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_ADT"=>"1", "ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketDay2"=>"20", "ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketMonth1"=>"2016-06", "ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketMonth2"=>"2016-06", "ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin1"=>"SGN", "ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListFareTypes"=>"I", "_pe_39b5379c652b_9df496572198"=>"null", "travel-indicator"=>"on", "ControlGroupSearchView$AvailabilitySearchInputSearchView$RadioButtonMarketStructure"=>"RoundTrip", "ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination1"=>"HAN", "pageToken"=>"sLkmnwXwAsY=", "culture"=>"vi-VN", "locale"=>"vi-VN", "currencyPicker"=>"VND", "ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListCurrency"=>"VND"}, :headers=>{"Accept-Encoding"=>"gzip, deflate", "Content-type"=>"application/x-www-form-urlencoded"}}

我的Http :: SearchFlightService

class Http::SearchFlightService
  include HTTParty
  ssl_version :SSLv3
  def self.search(url, options)
    post(url, options)
  end
end

显示错误

/Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/protocol.rb:153:in `read_nonblock': Connection reset by peer (Errno::ECONNRESET)
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/protocol.rb:153:in `rbuf_fill'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/protocol.rb:104:in `read'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:399:in `read'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:289:in `block in read_body_0'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:260:in `inflater'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:279:in `read_body_0'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:201:in `read_body'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:226:in `body'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:163:in `reading_body'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:1420:in `block in transport_request'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:1411:in `catch'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:1411:in `transport_request'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:1384:in `request'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:1377:in `block in request'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:853:in `start'
    from /Users/Dona/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:1375:in `request'

请给我一些建议。感谢

1 个答案:

答案 0 :(得分:0)

httparty似乎有一些错误。有同样的Errno::ECONNRESET: Connection reset by peer。通过将httparty调用替换为net/http

来修复
require 'net/http'
JSON.parse Net::HTTP.get(URI.parse(url))