Click here to Skip to main content
15,892,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 **********
XML
<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 ********************************

VB
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"
        'Dim ws As New ism.ncd.NCDServicesService

        'Dim xmlSignature As XmlElement = GetSigningToken(vInXML, vOutXML, vCertSubject)
        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", "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



VB
' Sign an XML file and save the signature in a new file.
   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
       ' Load the certificate from the certificate store.
       Dim cert As X509Certificate2 = GetCertificateBySubject(SubjectName)

       ' Create a new XML document.
       Dim doc As New XmlDocument()

       ' Format the document to ignore white spaces.
       doc.PreserveWhitespace = False

       ' Load the passed XML file using it's name.
       doc.Load(New XmlTextReader(FileName))

       ' Create a SignedXml object.
       Dim signedXml As New Xml.SignedXml(doc)

       ' Add the key to the SignedXml document.
       signedXml.SigningKey = cert.PrivateKey

       ' Create a reference to be signed.
       Dim reference As New Xml.Reference()
       reference.Uri = "#Body"

       ' Add an enveloped transformation to the reference.
       Dim env As New XmlDsigEnvelopedSignatureTransform()
       reference.AddTransform(env)

       ' Add the reference to the SignedXml object.
       signedXml.AddReference(reference)

       ' Create a new KeyInfo object.
       Dim keyInfo As New KeyInfo()

       ' Load the certificate into a KeyInfoX509Data object
       ' and add it to the KeyInfo object.
       ' Create an X509IssuerSerial object and add it to the
       ' KeyInfoX509Data object.

       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)

       ' Add the KeyInfo object to the SignedXml object.
       signedXml.KeyInfo = keyInfo

       ' Compute the signature.
       signedXml.ComputeSignature()

       ' Get the XML representation of the signature and save
       ' it to an XmlElement object.
       Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()

       ' Append the element to the XML document.
       doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, True))


       If TypeOf doc.FirstChild Is XmlDeclaration Then
           doc.RemoveChild(doc.FirstChild)
       End If

       ' Save the signed XML document to a file specified
       ' using the passed string.
       Dim xmltw As New XmlTextWriter(SignedFileName, New UTF8Encoding(False))
       Try
           doc.WriteTo(xmltw)

       Finally
           xmltw.Close()
       End Try

   End Sub
Posted
Updated 9-May-16 18:15pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900