【问题标题】:Call SOAP api form c# using action, location, endpoint & namespace使用操作、位置、端点和命名空间调用 SOAP api form c#
【发布时间】:2019-04-15 06:36:05
【问题描述】:

我从来没有用过soap api。

我有要求我必须调用soap api并将响应作为json(REST)api发送。

我有 Web Service API Location(...?wsdl)、Endpoint、Namespace & Soap action。

我还有用户名、密码和其他输入参数。

我不确定如何使用上述信息创建 soap Envelope 并从 c# 调用 api。

谁能建议我怎么做。

这是服务 GetRxHistory 我正在尝试调用 https://pharmacy.esihealthcaresolutions.com:9100/v4.0/RxHistoryService.svc?wsdl/GetRxHistory

【问题讨论】:

标签: c# asp.net wcf soap


【解决方案1】:

首先使用 References > Add > Service Reference 将服务引用添加到您的项目。在地址字段中输入 wsdl 文件的 url:

https://pharmacy.esihealthcaresolutions.com:9100/v4.0/RxHistoryService.svc?singleWsdl

您可以使用以下方法创建调用此 API 的客户端:

RxHistoryServiceContractClient client = new RxHistoryServiceContractClient();

然后您可以使用客户端对象调用服务上的各种操作。

client.xxxx = xxx;
client.xxx = xxx;

在你的情况下,你的用户名和密码应该是这样的:

client.ClientCredentials.UserName.UserName = "your username";
client.ClientCredentials.UserName.Password = "your password";

最后,要得到回复,你可以这样写:

try
    {
      _Client.Open();

您将在此处传递您的请求或客户端对象:

GetRxHistoryResponse _Response = _Client.{MethodToGetResponse}(client);

      _Client.Close();
    }
catch (Exception ex)  
    { 

    }

【讨论】:

【解决方案2】:

有没有办法从我拥有的数据中创建肥皂信封?

我们可以使用 Message 类(System.ServiceModel.Channels)静态方法,CreateMessage 方法。 我做了一个demo,希望对你有用。

class Program
    {
        static void Main(string[] args)
        {
            Product p = new Product()
            {
                ID = 1,
                Name = "Mango"
            };
            Message m=Message.CreateMessage(MessageVersion.Soap12, "mymessage", p);
            MessageHeader hd = MessageHeader.CreateHeader("customheader", "mynamespace", 100);
            m.Headers.Add(hd);
            Console.WriteLine(m);
        }
    }
    public class Product
    {
        public int ID { get; set; }
        public string Name { get; set; }
}

结果。 这是官方文档。
https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.channels.message.createmessage?view=netframework-4.7.2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多