HttpClient的&安培; SOAP(C#) [英] HttpClient & SOAP (C#)

查看:208
本文介绍了HttpClient的&安培; SOAP(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用HttpClient的类来发送SOAP消息:

I'm trying to use the HttpClient class to send a SOAP message:

通过REST这样做从的here ):

Doing so with REST seems easy (code from 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类似的事情。

I would like to do something similar with SOAP.

我有主机(的 http://opensearch.addi.dk/2.2/
和SOAP消息要发布:

I have the host (http://opensearch.addi.dk/2.2/) and the SOAP message to POST:

<?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>



...但怎么送呢?

... but how to send it ?

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

I'll admit that this is the first SOAP Web Service I have ever used so I may have no idea what I'm doing, but in its simplest form could it be something like :

            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静态方法(创建。 )。但我不能得到那个工作...

I assume that the SOAP message should somehow be created through a HttpContent static method like HttpContent.Create(..) but I can't get that to work ...

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

I know this is a dumb question but I still need help :) !

TIA ...

推荐答案

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

I needed to do this myself and since I couldn't find any answers online, here's what I worked out. This uses a simple SOAP calculator service with an 'Add' method that takes two numbers and returns the sum.

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的&安培; SOAP(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆