将签名的XML从VB发送到AXIS Web服务

编程入门 行业动态 更新时间:2024-10-28 09:18:48
本文介绍了将签名的XML从VB发送到AXIS Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们的服务提供商有一个AXIS网络服务,但我使用的是VB.Net。我需要开发一个程序来签署xml文件并将签名的xml文件发送到Web服务并获得结果。下面是签名的XML文件&我的VB代码。但该计划停止在 resp = req.GetResponse() 和抛出消息"500内部服务器错误"。我是否使用正确的方式将签名的xml文件发送到AXIS Web服务。 $ *******签名XML文件**********

< soapenv:Envelope xmlns:SOAP-SEC =" schemas.xmlsoap/soap/security/2000-12" xmlns:soapenv =" schemas.xmlsoap/soap/envelope/" xmlns:xsd =" www.w3/2001/XMLSchema" xmlns:xsi =" http:// www.w3/2001/XMLSchema-instance" > < soapenv:标题 > ; < SOAP-SEC:签名 soapenv:actor =" " soapenv:mustUnderstand =" 0" > < ds:签名 xmlns:ds =" www.w3/2000/09/xmldsig#" > < ds:SignedInfo > < ds:CanonicalizationMethod 算法 =" www.w3/TR/2001/REC-xml-c14n-20010315" / > < ds:SignatureMethod 算法 =" www.w3/2000/09/xmldsig#rsa-sha1" / > < ds:参考 URI =" #Body" > < ds:DigestMethod 算法 =" http: //www.w3/2000/09/xmldsig#sha1" / > < ds:DigestValue > qh4x0Zv1AYnnrEhxa / A4 + NWntCg = < / ds:DigestValue > < / ds:参考 > < / ds:SignedInfo > < ds:SignatureValue > fUEfD2O5pxtCrFVFfMjuRjpKr // D541cCA24mAcLZb7tV3u8ULrWswKtc4znEn4kwI / 2DckJjWeX 一个/ Gv3mEesUIO / SqwyZG90J / giuQfyhGWv9QEpeQLUv4K2ZxBGTEr0 + u2vjxPj1bImDLjlhWAxqBd b1mOfJT8m0py8h + q3Is = 的 < / ds:SignatureValue > < / ds:签名 > < / SOAP-SEC:签名 > < / soapenv:标题 > < soapenv:Body Id =" 身体" > < NCDRequest soapenv:encodingStyle =" schemas.xmlsoap/soap/encoding/" > < NCDREQBean href =" #id0" / > < / NCDRequest > < multiRef id =" id0" soapenc:root =" 0" soapenv:encodingStyle =" schemas.xmlsoap/soap/encoding/ " xsi:type = " ns1:NCDRequest" xmlns:ns1 =" urn:NCDServices" xmlns:soapenc =" schemas.xmlsoap/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:正文 > < / soapenv:Envelope >

******** VB代码**** ****************************

私人 Sub SendSOAP() Dim vCertSubject 正如 字符串 = " E = abc @ qbc,CN = TestCert,O = ABC"

Dim vInXML 作为 字符串 = " C:\InFile.xml"

昏暗 vOutXML String = " C:\ SignFile。 xml"

SignXmlFile(vInXML,vOutXML,vCertSubject) Dim xDoc 作为 新 System.Xml.XmlDocument xDoc.Load( " C:\ SignedFile.xml" ) Dim bytes As 字节 () $ bytes = System.Text.Encoding.UTF8.GetBytes(xDoc.InnerXml) Dim req 正如 Net.HttpWebRequest = Net.WebRequest.Create( " xyz/ 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 作为 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 作为 新 IO.StreamReader(stm) 我 .TextBox1.Text = r.ReadToEnd() MsgBox( " ; 已完成" ) 结束 Sub

' 签署XML文件并保存新文件中的签名。 Sub SignXmlFile( ByVal FileName 作为 字符串, ByVal SignedFileName 作为 字符串, ByVal SubjectName 正如 字符串 ) 如果 Nothing = FileName 然后 投掷 新 ArgumentNullException( " FileName" ) 结束 如果 如果 没什么 = SignedFileName 然后 投掷 新 ArgumentNullException( " SignedFileName" ) 结束 如果 如果 什么 = SubjectName 然后 抛出 新 ArgumentNullException( " SubjectName" ) 结束 ' 从证书存储区加载证书。 Dim cert 作为 X509Certificate2 = GetCertificateBySubject(SubjectName) ' 创建新的XML文档。 Dim doc As 新 XmlDocument() ' 格式化文档忽略空格。 doc.PreserveWhitespace = False ' 使用它的名称加载传递的XML文件。 doc.Load( 新 XmlTextReader(FileName)) ' 创建一个SignedXml对象。 Dim signedXml 正如 新 Xml.SignedXml(doc) ' 添加密钥到SignedXml文档。 signedXml.SigningKey = cert.PrivateKey ' 创建要签名的引用。 Dim 参考 作为 新 Xml.Reference() reference.Uri = " #Body" ' 广告d对参考的包络变换。 Dim env 作为 新 XmlDsigEnvelopedSignatureTransform() reference.AddTransform(env) ' 添加对SignedXml对象的引用。 signedXml.AddReference(reference) ' 创建新的KeyInfo对象。 Dim keyInfo As 新 KeyInfo() ' 加载证书INT o KeyInfoX509Data对象 ' 并将其添加到KeyInfo对象。 ' 创建X509IssuerSerial对象并将其添加到 ' KeyInfoX509Data对象。 昏暗 kdata 作为 新 KeyInfoX509Data(cert) Dim xserial 作为 X509IssuerSerial xserial.IssuerName = cert.IssuerName.ToString() xserial.SerialNumber = cert.SerialNumber KDATA .AddIssuerSerial(xserial.IssuerName,xserial.SerialNumber) keyInfo.AddClause(kdata) ' 将KeyInfo对象添加到SignedXml对象。 signedXml.KeyInfo = keyInfo ' 计算签名。 signedXml.ComputeSignature() ' 获取XML表示形式签名并保存 ' 它到XmlElement对象。 Dim xmlDigitalSignature 作为 XmlElement = signedXml.GetXm l() ' 将元素附加到XML文档。 doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, True )) 如果 TypeOf doc.FirstChild XmlDeclaration 然后 doc.RemoveChild(doc.FirstChild) 结束 如果 ' 将签名的XML文档保存到指定的文件 ' 使用传递的字符串。 Dim xmltw 作为 新 XmlTextWriter(SignedFileName, 新 UTF8Encoding( False )) /> 尝试 doc.WriteTo(xmltw) 最后 xmltw.Close() 结束 尝试 结束 Sub

解决方案

你真的在磁盘上有一个文件,你要发送吗?真的?

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="schemas.xmlsoap/soap/security/2000-12" xmlns:soapenv="schemas.xmlsoap/soap/envelope/" xmlns:xsd="www.w3/2001/XMLSchema" xmlns:xsi="www.w3/2001/XMLSchema-instance"><soapenv:Header><SOAP-SEC:Signature soapenv:actor="" soapenv:mustUnderstand="0"><ds:Signature xmlns:ds="www.w3/2000/09/xmldsig#"> <ds:SignedInfo> <ds:CanonicalizationMethod Algorithm="www.w3/TR/2001/REC-xml-c14n-20010315"/> <ds:SignatureMethod Algorithm="www.w3/2000/09/xmldsig#rsa-sha1"/> <ds:Reference URI="#Body"> <ds:DigestMethod Algorithm="www.w3/2000/09/xmldsig#sha1"/> <ds:DigestValue>qh4x0Zv1AYnnrEhxa/A4+NWntCg=</ds:DigestValue> </ds:Reference> </ds:SignedInfo> <ds:SignatureValue> fUEfD2O5pxtCrFVFfMjuRjpKr//D541cCA24mAcLZb7tV3u8ULrWswKtc4znEn4kwI/2DckJjWeXa/Gv3mEesUIO/SqwyZG90J/giuQfyhGWv9QEpeQLUv4K2ZxBGTEr0+u2vjxPj1bImDLjlhWAxqBdb1mOfJT8m0py8h+q3Is=</ds:SignatureValue> </ds:Signature></SOAP-SEC:Signature></soapenv:Header><soapenv:Body Id="Body"><NCDRequest soapenv:encodingStyle="schemas.xmlsoap/soap/encoding/"><NCDREQBean href="#id0"/></NCDRequest><multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="schemas.xmlsoap/soap/encoding/" xsi:type="ns1:NCDRequest" xmlns:ns1="urn:NCDServices" xmlns:soapenc="schemas.xmlsoap/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, 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("xyz/NCDRequest") 'req.Headers.Add("SOAPAction", "xyz/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

' 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

解决方案

Do you actually have a file on disk, that you're sending? Really?

更多推荐

将签名的XML从VB发送到AXIS Web服务

本文发布于:2023-10-11 22:14:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1482987.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   VB   XML   AXIS   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!