使用 C# 的 SOAP 客户端
SOAP Client using C#
我有 Soap 网络服务 https://test-submit.health263.systems:8081/apacewebservices/AMF1_0?wsdl。这有一个名为 process 的方法。我的问题是如何创建 soap 客户端来发送请求,即来自服务器的 submits/Retrieves 信息。请求的示例 XML 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Member xmlns="urn:apace:member:format:1.0">
<Request>
<Transaction>
<VersionNumber>1.0</VersionNumber>
<Number>434252-342234-6765</Number>
<SystemIdentifier>SYSTEM999</SystemIdentifier>
<DestinationCode>APACE</DestinationCode>
<ClientCountryCode>ZA</ClientCountryCode>
<Timestamp TimeZone="Africa/Johannesburg">20160601123456</Timestamp>
<TestIndicator>Y</TestIndicator>
<User>ProviderX/Jane Doe</User>
</Transaction>
<MembershipLookup>
<Funder>AFunder</Funder>
<WithMembershipNumber>
<MembershipNumber>123456789</MembershipNumber>
</WithMembershipNumber>
</MembershipLookup>
</Request>
</Member>
响应消息的结构如下:
<?xml version="1.0" encoding="UTF-8"?>
<Member xmlns="urn:apace:member:format:1.0">
<Response>
<Transaction>
<VersionNumber>1.0</VersionNumber>
<Number>434252-342234-6765</Number>
<Status>S</Status>
<Timestamp TimeZone="Africa/Johannesburg">20160601123500</Timestamp>
</Transaction>
<Membership NumberOfBeneficiaryRecords="2">
<Funder>AFunder</Funder>
<MembershipNumber>123456789</MembershipNumber>
<Beneficiary SequenceNumber="1">
<DependentCode>00</DependentCode>
<Type>P</Type>
<Status>A</Status>
<BiometricEnrolmentStatus>Y</BiometricEnrolmentStatus>
</Beneficiary>
<Beneficiary SequenceNumber="2">
<DependentCode>01</DependentCode>
<Type>D</Type>
<Status>A</Status>
<BiometricEnrolmentStatus>Y</BiometricEnrolmentStatus>
</Beneficiary>
</Membership>
</Response>
</Member>
请帮助我真的卡住了,没有使用 C# 的 SOAP 服务,我试过在线示例但是结构不符合我的消息结构更复杂
实际的消息结构结构如下:
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:apac="http://apace.systems/apacewebservices/"
xmlns:urn="urn:apace:member:format:1.1">
<soap:Header>
<apac:secureToken>Token66657752</apac:secureToken>
</soap:Header>
<soap:Body>
<apac:process>
<apac:request>
<Member
xmlns="urn:apace:member:format:1.1">
<Request>
<Transaction>
<VersionNumber>1.1</VersionNumber>
<Number>30074</Number>
<SystemIdentifier>LIFEHEALTH</SystemIdentifier>
<DestinationCode>HEALTH263</DestinationCode>
<ClientCountryCode>ZA</ClientCountryCode>
<Timestamp TimeZone="Africa/Johannesburg">20200705123456</Timestamp>
<TestIndicator>Y</TestIndicator>
<User>CIMSZW/Jane Doe</User>
</Transaction>
<MembershipLookup>
<IncludeDetail>Y</IncludeDetail>
<Funder>CIMSZWA</Funder>
<WithMembershipNumber>
<MembershipNumber>11117374</MembershipNumber>
<DependentCode>00</DependentCode>
</WithMembershipNumber>
</MembershipLookup>
</Request>
</Member>
</apac:request>
</apac:process>
</soap:Body>
</soap:Envelope>
如何使用安全密钥令牌创建 SOAP 信封和 Header 部分
您可以使用几个不同的系统库来创建 HttpWebRequest,创建自定义 xml,然后在发送之前将 xml 插入到您的请求中。 (system.net, system.xml.linq, system.io)
我能够访问您的 Web 服务,但出现 500 错误。 (希望您能看到日志,否则那不是您的!)
这是一个可以调用 SOAP 网络服务的简单 class。它几乎是您需要的 xml,但可能需要一些调整。如果您在制作自定义 xml 时遇到问题,这可能是一个可行的解决方案。
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml.Linq;
namespace ConsoleApp1
{
/// <summary>
/// Random class
/// </summary>
class Class1
{
/// <summary>
/// Function that calls a SOAP web service
/// </summary>
public void CallSOAP()
{
try
{
// Construct http post request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://test-submit.health263.systems:8081/apacewebservices/AMF1_0"));
request.Method = "POST";
request.ContentType = "application/xml";
request.Accept = "application/xml";
// Setting a namespace for your xml
// I'm not sure the timezone one is set up properly
XNamespace soap = "urn:apace:member:format:1.0";
XNamespace timezone = "TimeZone=\"Africa/Johannesburg\"";
// This constructs your xml using the LINQ library. I modeled after your demo, but needs tweaking as I get 500 error from server.
XElement requestXML =
new XElement(soap + "Member",
new XElement("Request",
new XElement("Transaction",
new XElement("VersionNumber", "1.0"),
new XElement("Number", "434252 - 342234 - 6765"),
new XElement("SystemIdentifier", "SYSTEM999"),
new XElement("DestinationCode", "APACE"),
new XElement("ClientCountryCode", "ZA"),
new XElement(timezone + "Timestamp", "20160601123456"),
new XElement("TestIndicator", "Y"),
new XElement("User", "ProviderX/Jane Doe")
),
new XElement("MembershipLookup",
new XElement("Funder", "AFunder"),
new XElement("WithMembershipNumber",
new XElement("MembershipNumber", 123456789)
)
)
)
);
// Convert the xml into a stream that we write to our request
byte[] bytes = Encoding.UTF8.GetBytes(requestXML.ToString());
request.ContentLength = bytes.Length;
using (Stream putStream = request.GetRequestStream())
{
putStream.Write(bytes, 0, bytes.Length);
}
// Execute the request and get an xml response "reader". You can read all xml at once or line by line
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
var streamData = reader.ReadToEnd();
}
}
catch (Exception ex)
{
// Write exception to console & wait for key press
Console.WriteLine(ex.Message + ex.StackTrace);
Console.ReadKey();
}
}
}
}
你有两个选择
- 添加为服务引用,它将生成
- 使用 Visual studio 开发人员提示并使用代理使用服务 class
svcutil.exe https://test-submit.health263.systems:8081/apacewebservices/AMF1_0?wsdl /t:code /n:*,SampleNamespace /o:C:\Service\sampleServiceProxy.cs /config:C:\Service\sampleService.config / ct:System.Collections.Generic.List`1
我有 Soap 网络服务 https://test-submit.health263.systems:8081/apacewebservices/AMF1_0?wsdl。这有一个名为 process 的方法。我的问题是如何创建 soap 客户端来发送请求,即来自服务器的 submits/Retrieves 信息。请求的示例 XML 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Member xmlns="urn:apace:member:format:1.0">
<Request>
<Transaction>
<VersionNumber>1.0</VersionNumber>
<Number>434252-342234-6765</Number>
<SystemIdentifier>SYSTEM999</SystemIdentifier>
<DestinationCode>APACE</DestinationCode>
<ClientCountryCode>ZA</ClientCountryCode>
<Timestamp TimeZone="Africa/Johannesburg">20160601123456</Timestamp>
<TestIndicator>Y</TestIndicator>
<User>ProviderX/Jane Doe</User>
</Transaction>
<MembershipLookup>
<Funder>AFunder</Funder>
<WithMembershipNumber>
<MembershipNumber>123456789</MembershipNumber>
</WithMembershipNumber>
</MembershipLookup>
</Request>
</Member>
响应消息的结构如下:
<?xml version="1.0" encoding="UTF-8"?>
<Member xmlns="urn:apace:member:format:1.0">
<Response>
<Transaction>
<VersionNumber>1.0</VersionNumber>
<Number>434252-342234-6765</Number>
<Status>S</Status>
<Timestamp TimeZone="Africa/Johannesburg">20160601123500</Timestamp>
</Transaction>
<Membership NumberOfBeneficiaryRecords="2">
<Funder>AFunder</Funder>
<MembershipNumber>123456789</MembershipNumber>
<Beneficiary SequenceNumber="1">
<DependentCode>00</DependentCode>
<Type>P</Type>
<Status>A</Status>
<BiometricEnrolmentStatus>Y</BiometricEnrolmentStatus>
</Beneficiary>
<Beneficiary SequenceNumber="2">
<DependentCode>01</DependentCode>
<Type>D</Type>
<Status>A</Status>
<BiometricEnrolmentStatus>Y</BiometricEnrolmentStatus>
</Beneficiary>
</Membership>
</Response>
</Member>
请帮助我真的卡住了,没有使用 C# 的 SOAP 服务,我试过在线示例但是结构不符合我的消息结构更复杂
实际的消息结构结构如下:
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:apac="http://apace.systems/apacewebservices/"
xmlns:urn="urn:apace:member:format:1.1">
<soap:Header>
<apac:secureToken>Token66657752</apac:secureToken>
</soap:Header>
<soap:Body>
<apac:process>
<apac:request>
<Member
xmlns="urn:apace:member:format:1.1">
<Request>
<Transaction>
<VersionNumber>1.1</VersionNumber>
<Number>30074</Number>
<SystemIdentifier>LIFEHEALTH</SystemIdentifier>
<DestinationCode>HEALTH263</DestinationCode>
<ClientCountryCode>ZA</ClientCountryCode>
<Timestamp TimeZone="Africa/Johannesburg">20200705123456</Timestamp>
<TestIndicator>Y</TestIndicator>
<User>CIMSZW/Jane Doe</User>
</Transaction>
<MembershipLookup>
<IncludeDetail>Y</IncludeDetail>
<Funder>CIMSZWA</Funder>
<WithMembershipNumber>
<MembershipNumber>11117374</MembershipNumber>
<DependentCode>00</DependentCode>
</WithMembershipNumber>
</MembershipLookup>
</Request>
</Member>
</apac:request>
</apac:process>
</soap:Body>
</soap:Envelope>
如何使用安全密钥令牌创建 SOAP 信封和 Header 部分
您可以使用几个不同的系统库来创建 HttpWebRequest,创建自定义 xml,然后在发送之前将 xml 插入到您的请求中。 (system.net, system.xml.linq, system.io)
我能够访问您的 Web 服务,但出现 500 错误。 (希望您能看到日志,否则那不是您的!)
这是一个可以调用 SOAP 网络服务的简单 class。它几乎是您需要的 xml,但可能需要一些调整。如果您在制作自定义 xml 时遇到问题,这可能是一个可行的解决方案。
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml.Linq;
namespace ConsoleApp1
{
/// <summary>
/// Random class
/// </summary>
class Class1
{
/// <summary>
/// Function that calls a SOAP web service
/// </summary>
public void CallSOAP()
{
try
{
// Construct http post request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://test-submit.health263.systems:8081/apacewebservices/AMF1_0"));
request.Method = "POST";
request.ContentType = "application/xml";
request.Accept = "application/xml";
// Setting a namespace for your xml
// I'm not sure the timezone one is set up properly
XNamespace soap = "urn:apace:member:format:1.0";
XNamespace timezone = "TimeZone=\"Africa/Johannesburg\"";
// This constructs your xml using the LINQ library. I modeled after your demo, but needs tweaking as I get 500 error from server.
XElement requestXML =
new XElement(soap + "Member",
new XElement("Request",
new XElement("Transaction",
new XElement("VersionNumber", "1.0"),
new XElement("Number", "434252 - 342234 - 6765"),
new XElement("SystemIdentifier", "SYSTEM999"),
new XElement("DestinationCode", "APACE"),
new XElement("ClientCountryCode", "ZA"),
new XElement(timezone + "Timestamp", "20160601123456"),
new XElement("TestIndicator", "Y"),
new XElement("User", "ProviderX/Jane Doe")
),
new XElement("MembershipLookup",
new XElement("Funder", "AFunder"),
new XElement("WithMembershipNumber",
new XElement("MembershipNumber", 123456789)
)
)
)
);
// Convert the xml into a stream that we write to our request
byte[] bytes = Encoding.UTF8.GetBytes(requestXML.ToString());
request.ContentLength = bytes.Length;
using (Stream putStream = request.GetRequestStream())
{
putStream.Write(bytes, 0, bytes.Length);
}
// Execute the request and get an xml response "reader". You can read all xml at once or line by line
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
var streamData = reader.ReadToEnd();
}
}
catch (Exception ex)
{
// Write exception to console & wait for key press
Console.WriteLine(ex.Message + ex.StackTrace);
Console.ReadKey();
}
}
}
}
你有两个选择
- 添加为服务引用,它将生成
- 使用 Visual studio 开发人员提示并使用代理使用服务 class
svcutil.exe https://test-submit.health263.systems:8081/apacewebservices/AMF1_0?wsdl /t:code /n:*,SampleNamespace /o:C:\Service\sampleServiceProxy.cs /config:C:\Service\sampleService.config / ct:System.Collections.Generic.List`1