NET :: HTTP帖子的问题

时间:2011-04-21 13:40:18

标签: ruby-on-rails ruby

我想将XML从我的服务器发布到其他服务器,所以我使用以下代码

#!/usr/bin/ruby
require 'net/http'
require 'cgi'
require "rubygems"

uuid = "_10.223.18.88.8734589732985756"
t= Time.now

soapEnvelop = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header/><SOAP-ENV:Body><samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\"  MajorVersion=\"1\" MinorVersion=\"1\" RequestID=\"#{uuid}\" IssueInstant=\"#{t}\"><samlp:AssertionArtifact>ST-askdb4YfcMasnH9nrItd-vm-cas2</samlp:AssertionArtifact></samlp:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>"

url = URI.parse('https://10.223.18.78/sso/samlValidate?TARGET=http://10.223.18.88/')
request = Net::HTTP::Post.new(url.path)

request.body="<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header/><SOAP-ENV:Body><samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\"  MajorVersion=\"1\" MinorVersion=\"1\" RequestID=\"#{uuid}\" IssueInstant=\"#{t}\"><samlp:AssertionArtifact>ST-askdb4YfcMasnH9nrItd-vm-cas2</samlp:AssertionArtifact></samlp:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>"
response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}

puts response

当我执行时,我收到以下错误:

/root/website/install/local/ruby-1.8.7-p299/lib/ruby/1.8/net/protocol.rb:135:in`sysread':已达到文件结尾(EOFError)

来自/root/website/install/local/ruby-1.8.7-p299/lib/ruby/1.8/net/protocol.rb:135:in`rbuf_fill'

    from /root/website/install/local/ruby-1.8.7-p299/lib/ruby/1.8/timeout.rb:62:in `timeout'

    from /root/website/install/local/ruby-1.8.7-p299/lib/ruby/1.8/timeout.rb:93:in `timeout'

    from /root/website/install/local/ruby-1.8.7-p299/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'

    from /root/website/install/local/ruby-1.8.7-p299/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'

    from /root/website/install/local/ruby-1.8.7-p299/lib/ruby/1.8/net/protocol.rb:126:in `readline'

    from /root/website/install/local/ruby-1.8.7-p299/lib/ruby/1.8/net/http.rb:2026:in `read_status_line'

    from /root/website/install/local/ruby-1.8.7-p299/lib/ruby/1.8/net/http.rb:2015:in `read_new'

    from /root/website/install/local/ruby-1.8.7-p299/lib/ruby/1.8/net/http.rb:1051:in `request'

    from samlRequest.rb:17

    from /root/website/install/local/ruby-1.8.7-p299/lib/ruby/1.8/net/http.rb:543:in `start'

    from /root/website/install/local/ruby-1.8.7-p299/lib/ruby/1.8/net/http.rb:440:in `start'

    from samlRequest.rb:17

不确定问题是什么。请帮忙.. 提前谢谢。

2 个答案:

答案 0 :(得分:2)

由于这显然是一个SOAP交换,我建议使用Savon或SOAP4R,而不是滚动自己的SOAP客户端。这将为您提供更多功能,并且不再复杂(可能更少,取决于您尝试做什么)。有关详细信息,请查看Savon documentation

答案 1 :(得分:0)

因为它是https,你还应该:

require 'net/https'

request = Net::HTTP::Post.new(url.path)
request.use_ssl = true

不确定它是否会解决您的问题。请告诉我们结果