这就是我目前设法使用特定 Microsoft Web 服务的方式。请注意,它位于 HTTPS 服务器上,并且需要用户名、密码和 .cer
文件才能安装在操作系统的“根证书颁发机构”中。
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.Security.Message.NegotiateServiceCredential = true;
binding.Security.Message.AlgorithmSuite
= System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
binding.Security.Message.EstablishSecurityContext = true;
EndpointAddress endpoint = new EndpointAddress("https://address.of.service");
//"GreatClient" was created for me automatically by running
//"svcutil.exe https://address.of.service?wsdl"
GreatClient client = new GreatClient(binding, endpoint);
//Username and password for the authentication. Notice that I have also installed
//the required .cer certificate into the system's "root certificate authorities".
client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "password";
//Now I can start using the client as I wish.
我的问题是:我怎样才能获得所有必要的信息,以便我可以通过直接 POST 到 https://address.of.service 来使用 Web 服务,以及如何使用 C# 实际执行 POST?我只想使用 POST,我可以使用 POST 直接向 https://address.of.service 提供原始 XML 数据。并将结果作为原始 XML 数据取回。问题是,原始 XML 数据是什么,我应该如何使用 POST 发送它?
(这个问题的目的:我问的原因是我希望使用 C# 和 .NET 以外的东西(例如 Ruby 或 Mac OS X 上的 Cocoa)来使用此服务。我不知道到底该怎么做,因为我在其他平台上没有任何易于使用的“svcutil.exe”来为我生成所需的代码。这就是为什么我认为只要能够使用常规 POST 使用服务将使我能够更轻松地在其他平台上使用服务。)
最佳答案
您现在尝试做的事情听起来很痛苦,如果服务器发生任何变化,继续前进也很痛苦。这真的是在重新发明轮子。
如果你还没有考虑过,我会:
(a) 研究您是否可以将您拥有的元数据用于该服务,并使用目标平台原生的代理生成器。没有多少平台没有至少一些工具,即使不是全部,也可能让您参与其中。也许重新发布一个针对 Ruby 用户的问题,询问存在哪些框架来使用 HTTPS 服务,因为它是 WSDL?
(b) 如果做不到这一点,如果您的方案允许,我会考虑使用用 C# 编写的代理,它充当服务的外观,将其转换为更易于使用的内容(例如,您可以使用 ASP. NET MVC WebAPI,它很灵活,可以轻松提供符合标准的响应,您可以保持对这些响应的完全控制。
我怀疑其中之一可能会证明比您目前所走的道路更容易、更有值(value)。
关于c# - 使用 POST 而不是通常的 WSDL 路由来使用 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9960870/