HttpClient和SOAP(C#)

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

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

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().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消息:

     dc.title=zorro AND dc.type=bog 100200 test 1 10    

……但是怎么发送呢?

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

  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 …

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

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