将WCF(svcutil)C#代码转换为相应的SOAP信封POST请求 [英] Converting a WCF (svcutil) C# code into the corresponding SOAP envelope POST request

查看:166
本文介绍了将WCF(svcutil)C#代码转换为相应的SOAP信封POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前(并成功)连接到C#中的WCF Web服务的方式. 我对此网络服务没有任何控制权,因为它不是我开发的,因此我无法对其进行更改.这是我使用的C#代码:

This is how I'm currently (and successfully) connecting to a WCF web service in C#. I do not have any control over this web service as it's not developed by me, so I cannot change it. Here is the C# code I use:

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("<address>");

fooClient client = new fooClient(binding, endpoint);
client.ClientCredentials.UserName.UserName = "the_username";
client.ClientCredentials.UserName.Password = "the_password";

//fooClient class came from running
//    svcutil.exe https://<thedomain>/foo/foo.svc?wsdl

//I now work with fooClient, call methods on it, etc.


我想在不使用C#的情况下连接到Web服务-通过手动创建SOAP信封并在端点上执行POST请求.我尝试做一个看起来像这样的POST请求:


I want to connect to the web service without C# - by manually creating a SOAP envelope and doing a POST request on the endpoint. I tried doing a POST request that looks like this:

POST /foo/foo.svc HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8
Host: <thedomain>
Connection: close
User-Agent: <my user agent>
Content-Length: 416

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">

  <soap12:Body>
    <someWebServiceFunction xmlns="http://<thedomain>/foo/foo">
      <someParameter>some parameter value</someParameter>
    </someWebServiceFunction>
  </soap12:Body>

</soap12:Envelope>

但是这不起作用,因为缺少凭据. (我收到一个错误消息:"BadContextToken",安全上下文令牌已过期或无效.消息未得到处理.")

But this does not work because the credentials are missing. (I get an error back: "BadContextToken", "The security context token is expired or is not valid. The message was not processed.")

我的问题是,如何将凭据添加到SOAP信封/HTTP请求?我尝试做普通的HTTP基本身份验证(在Authorization HTTP标头中),但这继续给我同样的"BadContextToken"错误.

My question is, how do I add credentials to my SOAP envelope / HTTP request? I tried doing plain HTTP Basic Auth (in the Authorization HTTP header), but this continues to give me the same "BadContextToken" error.

推荐答案

有两种简单的方法可以解决问题:

There are two simple ways to trouble shoot your issue:

  1. 如果使用的是Visual Studio,请在调试中发送请求并截取请求中的详细内容,然后可以使用纯HTTP POST模拟它.

  1. If you are using Visual Studio, in debug, send the request and intercept what is the detailed content in the request, and you can simulate it using plain HTTP POST.

如果可以使用SoapUI,则可以使用SoapUI定位服务并发送一个工作请求,在原始选项卡中,您将看到什么是带有凭据的已接受请求.

If you can use SoapUI, you can target the service using SoapUI and send one working request, in the raw tab, you will see what's the accepted request with credentials.

这篇关于将WCF(svcutil)C#代码转换为相应的SOAP信封POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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