我使用下面的一個,你可以使用GET和POST。
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
這個文檔不夠清楚嗎? –
@Sergio:我可以知道你指的是哪個文件嗎? –
POST操作我的代碼應該採取2個輸入,即目標網址和XML請求主體。 –