Problem with ruby net::HTTP issuing a SOAP request
First of开发者_运维百科f, sorry for what I know is terrible idiomatic Ruby code.
I am trying to issue a series of SOAP requests to Oracle CRM OnDemand using a ruby script and I am running into a problem. I can issue the requests just fine using Poster for FireFox, but when I try and issue them with Ruby, it kicks back the following:
Internal Error: Session is not available. Aborting.
Oracle CRM OnDemand requires an authorization session cookie. Below is the code that I am using:
httpOracle = Net::HTTP.new(ORACLE_BASE_URL, ORACLE_PORT)
httpOracle.use_ssl = true
httpOracle.verify_mode = OpenSSL::SSL::VERIFY_NONE
httpOracle.set_debug_output $stderr
begin
# CONNECT TO ORACLE AND RETRIEVE A SESSION ID
pathOracle = buildOracleLoginPath()
headOracle = { "username" => ORACLE_USERNAME,
"password" => ORACLE_PASSWORD }
respOracle = httpOracle.request_head(pathOracle, headOracle)
authOracle = respOracle['set-cookie']
.gsub(/ /, '')
.split(';')
.find_all { |item| item.match(/^JSESSIONID=/) }[0].to_s
# RETRIEVE ALL ORACLE LEADS
pathOracle = buildOraclePath(authOracle)
headOracle = { "soapaction" => buildOracleSOAPAction("Lead", "QueryPage"),
"Content-Type" => "text/xml" }
rqstOracle = loadPostData 'soap.xml' # Loads file with SOAP payload as a string
respOracle = httpOracle.request_post(pathOracle, rqstOracle, headOracle)
puts respOracle # for testing
rescue
puts "Error #{$!}"
ensure
# CLOSE THE CONNECTION TO ORACLE
pathOracle = buildOracleLogoffPath()
headOracle = { authOracle.split('=')[0] => authOracle.split('=')[1] }
respOracle = httpOracle.request_head(pathOracle, headOracle)
end
I can take the outputs of each of those commands and pump it through Poster (login, query, logoff) and it will work flawlessly, but for some reason, it looks like something is wrong having it bundled together in a script.
I was wondering if maybe trying to use the same Net::HTTP for multiple connections is the problem? Or maybe I am just not using it the right way?
If someone needs it, I can try and figure out how to redirect the http output to a file so that you can see the postings if that is helpful.
Thanks!
I guess that typing it out gave me some more search criteria to work off of and I stumbled on this blog post. Although I would still like to know what I was doing wrong (I think that I have to use something like http.start {} to make this work), and how my code differs from clicking the buttons in Poster, adding WS-SECURITY headers to the SOAP request solved the problem completely by making it a stateless request instead of having to maintain the state throughout the life of the process.
Thanks to anyone who took the time to read this!
精彩评论