我使用了以下一个,您可以使用它来获取和发布。
def invoke_webservice url
query_string = url[:query_string].blank? ? "" : url[:query_string].map{ |key, value|
"#{key}=#{CGI.escape(value)}" }.join("&").insert(0, "?")
url[:class] = url[:class].blank? ? "" : url[:class]
webservice_uri = URI.join(APP_CONFIG['webservice_uri_format'], '/application/',
url[:class], url[:action], query_string)
uri = URI.parse(webservice_uri.to_s)
http = Net::HTTP.new(uri.host, uri.port)
#http.use_ssl = true
#http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.open_timeout = 25
http.read_timeout = 500
http.start do |http|
if url[:method] == 'get'
request = Net::HTTP::Get.new(uri.request_uri)
else
request = Net::HTTP::Post.new uri.path, initheader = { 'Content-Type' => 'application/json' }
request.body = url[:data].to_json
end
request.basic_auth APP_CONFIG['username'], APP_CONFIG['password']
response = http.request request
JSON.parse(response.body) if response.code =~ /^2\d\d$/
end
rescue => e
puts "Error Message is #{e.inspect} #{e.backtrace}"
end
end