📅  最后修改于: 2022-03-11 14:49:16.850000             🧑  作者: Mango
public async Task AddNumbersAsync(Uri uri, int a, int b)
{
var soapString = this.ConstructSoapRequest(a, b);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("SOAPAction", "http://CalculatorService/ICalculatorService/Add");
var content = new StringContent(soapString, Encoding.UTF8, "text/xml");
using (var response = await client.PostAsync(uri, content))
{
var soapResponse = await response.Content.ReadAsStringAsync();
return this.ParseSoapResponse(soapResponse);
}
}
}
private string ConstructSoapRequest(int a, int b)
{
return String.Format(@"
{0}
{1}
", a, b);
}
private int ParseSoapResponse(string response)
{
var soap = XDocument.Parse(response);
XNamespace ns = "http://CalculatorService/";
var result = soap.Descendants(ns + "AddResponse").First().Element(ns + "AddResult").Value;
return Int32.Parse(result);
}