Hi,
Our service provider has a AXIS web service but I am using VB.Net. I need to develop a program to sign the xml file and send the signed xml file to the web service and get the result. Below is the Signed XML file & my VB codes. But the program stop at
resp = req.GetResponse()
and throw a message "500 interal server error". Am I using the correct way to send the signed xml file to AXIS web service.
******* Signed XML File **********
<soapenv:Envelope xmlns:SOAP-SEC="http://schemas.xmlsoap.org/soap/security/2000-12" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Header><SOAP-SEC:Signature soapenv:actor="" soapenv:mustUnderstand="0"><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="#Body">
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>qh4x0Zv1AYnnrEhxa/A4+NWntCg=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>
fUEfD2O5pxtCrFVFfMjuRjpKr//D541cCA24mAcLZb7tV3u8ULrWswKtc4znEn4kwI/2DckJjWeX
a/Gv3mEesUIO/SqwyZG90J/giuQfyhGWv9QEpeQLUv4K2ZxBGTEr0+u2vjxPj1bImDLjlhWAxqBd
b1mOfJT8m0py8h+q3Is=
</ds:SignatureValue>
</ds:Signature></SOAP-SEC:Signature></soapenv:Header><soapenv:Body Id="Body"><NCDRequest soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><NCDREQBean href="#id0"/></NCDRequest><multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns1:NCDRequest" xmlns:ns1="urn:NCDServices" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"><coverType xsi:type="soapenc:string">3</coverType><curChassisNo xsi:type="soapenc:string">NA</curChassisNo><curVehNo xsi:type="soapenc:string">NA</curVehNo><insCode xsi:type="soapenc:string">235</insCode><preAppCode xsi:type="soapenc:string">NA</preAppCode><preInsCode xsi:type="soapenc:string">422</preInsCode></multiRef></soapenv:Body></soapenv:Envelope>
******** VB CODE ********************************
Private Sub SendSOAP()
Dim vCertSubject As String = "E=abc@qbc.com, CN=TestCert, O=ABC"
Dim vInXML As String = "C:\InFile.xml"
Dim vOutXML As String = "C:\SignedFile.xml"
SignXmlFile(vInXML, vOutXML, vCertSubject)
Dim xDoc As New System.Xml.XmlDocument
xDoc.Load("C:\SignedFile.xml")
Dim bytes As Byte()
bytes = System.Text.Encoding.UTF8.GetBytes(xDoc.InnerXml)
Dim req As Net.HttpWebRequest = Net.WebRequest.Create("https://xyz.com/NCDRequest")
req.Headers.Add("SOAPAction", "urn:NCDServices" & "#" & "NCDRequest")
req.ContentType = "text/xml;charset=utf-8"
req.Accept = "text/xml"
req.Method = "POST"
Dim stm As IO.Stream
stm = req.GetRequestStream()
stm.Write(bytes, 0, bytes.Length)
stm.Close()
Dim resp As Net.WebResponse
resp = req.GetResponse()
stm = resp.GetResponseStream()
Dim r As New IO.StreamReader(stm)
Me.TextBox1.Text = r.ReadToEnd()
MsgBox("completed")
End Sub
Sub SignXmlFile(ByVal FileName As String, ByVal SignedFileName As String, ByVal SubjectName As String)
If Nothing = FileName Then
Throw New ArgumentNullException("FileName")
End If
If Nothing = SignedFileName Then
Throw New ArgumentNullException("SignedFileName")
End If
If Nothing = SubjectName Then
Throw New ArgumentNullException("SubjectName")
End If
Dim cert As X509Certificate2 = GetCertificateBySubject(SubjectName)
Dim doc As New XmlDocument()
doc.PreserveWhitespace = False
doc.Load(New XmlTextReader(FileName))
Dim signedXml As New Xml.SignedXml(doc)
signedXml.SigningKey = cert.PrivateKey
Dim reference As New Xml.Reference()
reference.Uri = "#Body"
Dim env As New XmlDsigEnvelopedSignatureTransform()
reference.AddTransform(env)
signedXml.AddReference(reference)
Dim keyInfo As New KeyInfo()
Dim kdata As New KeyInfoX509Data(cert)
Dim xserial As X509IssuerSerial
xserial.IssuerName = cert.IssuerName.ToString()
xserial.SerialNumber = cert.SerialNumber
kdata.AddIssuerSerial(xserial.IssuerName, xserial.SerialNumber)
keyInfo.AddClause(kdata)
signedXml.KeyInfo = keyInfo
signedXml.ComputeSignature()
Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()
doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, True))
If TypeOf doc.FirstChild Is XmlDeclaration Then
doc.RemoveChild(doc.FirstChild)
End If
Dim xmltw As New XmlTextWriter(SignedFileName, New UTF8Encoding(False))
Try
doc.WriteTo(xmltw)
Finally
xmltw.Close()
End Try
End Sub