【问题标题】:HttpClient & SOAP (C#)HttpClient 和 SOAP (C#)
【发布时间】:2013-01-15 11:08:23
【问题描述】:

我正在尝试使用 HttpClient 类发送 SOAP 消息:

使用 REST 这样做似乎很容易(代码来自 here):

using System;
using System.Net.Http;
using System.Json;

namespace ConsoleApplication39
{
class Program
{
static void Main(string[] args)
{

HttpClient proxy = new HttpClient();
proxy.GetAsync("http://localhost:14892/api/Bloggers").ContinueWith((r) =>
{
HttpResponseMessage response = r.Result;
response.Content.ReadAsAsync<JsonArray>().ContinueWith(
(a)=>
{
foreach(var w in a.Result)
{
Console.WriteLine(w.ValueOrDefault("Name").ToString());
Console.WriteLine(w.ValueOrDefault("Intrest").ToString());
}
});

});

Console.ReadKey(true);

}

}
}

我想用 SOAP 做一些类似的事情。

我有主持人 (http://opensearch.addi.dk/2.2/) 以及要 POST 的 SOAP 消息:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://oss.dbc.dk/ns/opensearch">
  <SOAP-ENV:Body>
    <ns1:searchRequest>
      <ns1:query>dc.title=zorro AND dc.type=bog</ns1:query>
      <ns1:agency>100200</ns1:agency>
      <ns1:profile>test</ns1:profile>
      <ns1:start>1</ns1:start>
      <ns1:stepValue>10</ns1:stepValue>
    </ns1:searchRequest>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

...但是如何发送呢?

我承认这是我用过的第一个 SOAP Web 服务,所以我可能不知道自己在做什么,但最简单的形式可能是这样的:

HttpClient hc = new HttpClient();
hc.BaseAddress = new Uri("http://opensearch.addi.dk/2.2/");
    
HttpContent content = *... something*
    
HttpResponseMessage rm = await hc.PostAsync("http://opensearch.addi.dk/2.2/", content);

我假设 SOAP 消息应该以某种方式通过 HttpContent.Create(..) 之类的 HttpContent 静态方法创建,但我无法让它工作......

我知道这是一个愚蠢的问题,但我仍然需要帮助:)!

tia ...

【问题讨论】:

  • 这个答案可能有用; stackoverflow.com/a/4791932/5827
  • 这可能很有用,但我希望使用 HttpClient 以获得更大的简单性和强大的功能......其他代码似乎过于复杂和具体。我希望将 HttpClient 用于许多事情(不仅仅是 SOAP),并且想要一个更通用的解决方案!

标签: c# soap httpclient


【解决方案1】:

我需要自己做这个,因为我在网上找不到任何答案,这就是我的工作。这使用了一个简单的 SOAP 计算器服务和一个“加法”方法,该方法接受两个数字并返回总和。

public async Task<int> 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(@"<?xml version=""1.0"" encoding=""utf-8""?>
    <s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
        <s:Body>
            <Add xmlns=""http://CalculatorService/"">
                <a>{0}</a>
                <b>{1}</b>
            </Add>
        </s:Body>
    </s:Envelope>", 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);
}

【讨论】:

  • 很好的答案,请注意HttpClient 旨在创建一次并重复用于具有相同配置的许多请求。因此,我建议您创建一个静态成员,并在多次调用您的示例AddNumbersAsync 时重复使用它
  • @Alireza 有效点。我只是用我的代码来测试一些东西,所以这对我来说并不重要,但对于更正式的服务来说,这肯定是正确的决定。
  • 非常有用的答案。使用 PostAsync 处理 XML 消息时遇到了困难。你给了我一个直接使用 SoapString 的想法。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 2023-04-09
  • 1970-01-01
  • 2011-08-25
  • 1970-01-01
相关资源
最近更新 更多