2016-08-19 36 views
1

我一直试图通过谷歌我的方式,但我最终在这个问题上挠了挠头。使用UWP将信息保存到SOAP

当谈到代码时,我是一个缓慢的学习者,但我并没有真正放弃。只要有很好的文档,我通常可以通过我的方式学习。

我目前的问题解决了从UWP应用程序发送数据到SOAP服务。提取数据很容易找到有关文件。但是储蓄似乎是一个完全不同的问题。

我也试过在这里通过最后两天找到它,但没有多少给出。

任何人都可以给我提示或示例代码,我们使用C#将数据从UWP应用程序发送到SOAP服务?

我想发送数据的XAML代码行非常简单。

<textbox>Int</textbox 
<textbox>Decimal?</textbox 
<textbox>String</textbox 
<button name=send /> 

回答

0

谁能给我提示或在这里我们使用C#从一个UWP应用程序将数据发送到SOAP服务的示例代码?

首先,您可能会看到如何从SOAP服务中调用数据,常见的方法是使用HttpClient类。

如果需要发送数据时,也使用这个类,我们需要构建从后面的代码SOAP请求,这里是一个简单的演示:

测试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); 
    }