在Visual Studio c#中连接到SOAP WSDL
本文关键字:SOAP WSDL 连接 Visual Studio | 更新日期: 2023-09-27 18:04:25
我正在尝试将我的c#控制台应用程序与第三方SOAP WSDL API连接,但是我无法使其工作。我使用WSDL字符串连接到服务,作为Visual studio中的服务追索权:https://domain.tld/SimpleAPI/SimpleAPIService.svc?singleWsdl
文档说:身份验证是基于会话的。请求者必须在每个请求的Authenticate字段中提供有效的会话令牌。会话可以访问与给定用户相同的资源和功能。要获取会话令牌,请使用GetSessionToken方法。要验证会话,请使用PingPong方法。文档关注的是REST API,这对我来说不是一个选项。两种连接方法如下:
curl ' -H "Content-Type: application/json" ' -X POST -d '{ "username": "admin@domain.com", "password": "VerySecure123" }' ' https://domain.tld/SimpleAPI/SimpleAPIService.svc/rest/GetSessionToken
这应该返回如下内容:C9C5MlqrpMwG/6bQ3mAVlm0Z6hi/eh1vsQs4i1I/g==这部分没问题,我可以拿回一个代币。然而,当我尝试乒乓方法时,我得到一个错误,说它是一个错误的令牌或会话已经过期。
curl ' -H "Content-Type: application/json" ' -H "Authenticate:CCPSessionId C9C5MlqrpMwG/6bQ3mAVlm0Z6hi/eh1vsQs4i1I/g==" ' -X POST -d '{}' ' https://domain.tld/SimpleAPI/SimpleAPIService.svc/rest/PingPong
我的代码如下:
static void Main(string[] args)
{
string apiUsername = "user";
string apiPassword = "pass";
using (var client = new acmp.SimpleAPIServiceClient())
{
client.ClientCredentials.UserName.UserName = apiUsername;
client.ClientCredentials.UserName.Password = apiPassword;
string token = client.GetSessionToken(apiUsername, apiPassword);
string tokenText = string.Format("CCPSessionId {0}", token);
using (OperationContextScope contextScope = new OperationContextScope(client.InnerChannel))
{
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[HttpRequestHeader.Authorization] = token; //tokenText
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
client.PingPong();
}
}
Console.WriteLine("End...");
Console.ReadLine();
}
现在的理论是,我的脚本没有传递正确的头。
请帮忙,我们花了这么多时间调试
做了一点阅读,我找不到一个标记文本"CCPSessionsid "的任何地方。
我看到"bearer", token。AccessToken
沿着:
client.DefaultRequestHeaders。Authorization = new AuthenticationHeaderValue("bearer", token.AccessToken);
我在使用相同的API时也遇到了这个问题。把引号从字符串中去掉为我解决了这个问题。
request.Headers.TryAddWithoutValidation("Authenticate", token.Trim(new Char[] {'"'}));