我一直在尝试通过谷歌搜索,但最终还是对这个问题有些摸不着头脑。
在代码方面,我学得比较慢,但我并没有真正放弃。只要有良好的文档,我通常可以自己学习。
我当前的问题解决了将数据从 UWP 应用程序发送到 SOAP 服务的问题。获取数据很容易找到相关文档。但储蓄似乎是完全不同的事情。
过去两天我也试图在这里找到它,但没有给出太多。
在我们使用 C# 将数据从 UWP 应用程序发送到 SOAP 服务时,谁能给我提示或示例代码?
我要发送数据的 XAML 代码行非常简单。
<textbox>Int</textbox
<textbox>Decimal?</textbox
<textbox>String</textbox
<button name=send />
最佳答案
Can anyone give me tips or an example code where we use C# to send data from an UWP app to a SOAP service?
首先,您可能会看到如何从 SOAP 服务调用数据,常见的方式是使用 HttpClient
类。
如果需要发送数据,也使用这个类,我们需要在后面的代码中构造Soap请求,这里是一个简单的demo:
测试 SOAP 请求如下:
<?xml version=""1.0"" encoding=""utf-8""?>
<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
<s:Body>
<Save xmlns=""http://TestService/"">
<textbox>txt1</textbox>
<textbox>txt2</textbox>
<textbox>txt3</textbox>
</Save>
</s:Body>
</s:Envelope>
代码示例:
private async void btn_Send_Click(object sender, RoutedEventArgs e)
{
await AddNumbersAsync(new Uri("http://xxxxxx/TestService.asmx"), "txt1", "txt2", "txt3");
}
public async Task<int> AddNumbersAsync(Uri uri, string t1, string t2, string t3)
{
var soapString = this.ConstructSoapRequest(t1, t2, t3);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("SOAPAction", "http://TestServiceService/ITestServiceService/Save");
var content = new HttpStringContent(soapString, Windows.Storage.Streams.UnicodeEncoding.Utf8, "text/xml");
using (var response = await client.PostAsync(uri, content))
{
var soapResponse = await response.Content.ReadAsStringAsync();
return this.ParseSoapResponse(soapResponse);
}
}
}
private string ConstructSoapRequest(string t1, string t2, string t3)
{
return String.Format(@"<?xml version=""1.0"" encoding=""utf-8""?>
<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
<s:Body>
<Save xmlns=""http://TestService/"">
<textbox>{0}</textbox>
<textbox>{1}</textbox>
<textbox>{2}</textbox>
</Save>
</s:Body>
</s:Envelope>", t1, t2, t3);
}
private int ParseSoapResponse(string response)
{//Custom this function based on your SOAP response
var soap = XDocument.Parse(response);
XNamespace ns = "http://TestService/";
var result = soap.Descendants(ns + "SaveResponse").First().Element(ns + "SaveResult").Value;
return Int32.Parse(result);
}
关于c# - 使用 UWP 将信息保存到 SOAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39039777/