通过使用Ruby的http post来正确地url编码xml_data?我在下面('Content-Length')代码尝试了不同的URI :: encode(xml)和xml.URI.encode_www_form变种。然而,在事实之后,URI :: encode(xml)工作正常,但我需要在http帖子体内发送urlencoded xml数据,以使其正常工作。
# Server connection
uri = URI.parse(url)
req = Net::HTTP.new(uri.hostname, uri.port)
# Start test:
puts "\n\nStarting test on: #{url}\n"
# URL encoded SAML POST
SAMLResponse = %{
<samlp:Response
xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
ID="identifier_2"
Version="2.0"
IssueInstant="instant"
Destination="http://localhost:80">
<saml:Issuer>http://localhost:80:Issuer>
<samlp:Status>
<samlp:StatusCode
Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</samlp:Status>
<saml:Assertion
ID="ee4432"
Version="6.0"
IssueInstant="instant">
<saml:Issuer>http://localhost:80:Issuer>
<saml:Subject>
<saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid- vformat:unspecified">test_user</saml:NameID>
<saml:SubjectConfirmation
</saml:AttributeStatement>
</saml:Assertion>
</samlp:Response>
}
# HTTP POST | URL Encoded
res = req.post(
uri.path,
SAMLResponse,
{
'Content-Type' => 'text/xml',
'Content-Length' => SAMLResponse.length.to_s,
#'Content-Length' => URI::encode(SAMLResponse), // not working
#'Content-Length' => SAMLResponse.URI.encode_www_form, // not working
"Connection" => "keep-alive"
}
)
#puts res.body # uncomment once url encoding works
puts SAMLResponse