将签名的XML从VB发送到AXIS Web服务 [英] Send signed XML from VB to AXIS web service

查看:57
本文介绍了将签名的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     ="   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:标题   > ;  <     SOAP-SEC:签名    soapenv:actor     ="  "     soapenv:mustUnderstand     ="   0"  >  <     ds:签名    xmlns:ds     ="   http://www.w3.org/2000/09/xmldsig#"  >   
< ds:SignedInfo >
< ds:CanonicalizationMethod 算法 =" http://www.w3.org/TR/2001/REC-xml-c14n-20010315" / >
< ds:SignatureMethod 算法 <跨度> =" <跨度> http://www.w3.org/2000/09/xmldsig#rsa-sha1" <跨度> / >

< ds:参考 URI =" #Body" >
< ds:DigestMethod 算法 =" http: //www.w3.org/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:标题 > < < SP an> soapenv:Body
Id <跨度> =" <跨度>身体" <跨度>> <跨度>< 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 < span style ="color:#ff0000"> 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.com,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"



< span style ="color:#0000ff"> Dim bytes As
字节 ()
$
bytes = System.Text.Encoding.UTF8.GetBytes(xDoc.InnerXml)



Dim req 正如 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 作为 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 作为 字符串, < span style ="color:#0000ff"> 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="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", "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

' 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服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆