从C#调用PHP webservice并在SOAPHeader中传递凭据 [英] Call PHP webservice from C# and pass credentials in SOAPHeader

查看:88
本文介绍了从C#调用PHP webservice并在SOAPHeader中传递凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好, 
$


我获得了外部网络服务wsdl网址。 

网络服务是用php编写的。 

每次请求我都必须发送我的凭据。 



 < soapenv:Header>

  &NBSP; &NBSP; < AuthHeader>

  &NBSP; &NBSP; &NBSP;  < login> login< / login>

  &NBSP; &NBSP; &NBSP;  < password> password< / password>

  &NBSP; &NBSP; < / AuthHeader>

< / soapenv:标题>



$


我根据此链接播放使用SOAPHEADERS的示例

https://msdn.microsoft.com/en-us/library/77hkfhh8(VS.71).aspx

并且已经创建了所有内容。 

凭证被分配到服务类但是它不起作用。



我是用PHP不太好,所以我想知道:

是否有可能在提供商PHP代码上没有定义[SoapHeader("AuthHeader")]上面的方法? 



我在导入wsdl之后在生成的代码中添加了这个标题(在方法之前)。



你有什么建议吗? />



$

编辑: 

我找到了解决方案。问题在于为凭据命名这些字段。 

webservice的提供商未定义正确的标题名称。 

重命名字段后,它完美运行。



非常感谢您的时间和回复。 
$



解决方案

不确定这是否有帮助。 &NBSP;我刚刚使用
创建了一个web服务SOAP连接
.Net Core 2.0 < /a>. 它使用WSDL连接到Salesforce。 这是一个示例。 它使用异步方法。

使用System; 
使用SFDC;使用System.ServiceModel
;
使用System.Threading.Tasks;
使用System.IO;
使用System.Collections.Generic;

名称空间SfLogin
{
class Program
{
static string userName ="" ;;
static string password ="" ;;
static string securityToken ="" ;;
静态SoapClient loginSc;
静态SoapClient sc;
静态LoginResult lir;
静态SessionHeader头;
静态EndpointAddress端点;
静态布尔成功;

static void Main(string [] args)
{
loginSc = new SoapClient();
sc = new SoapClient();
login()。Wait();
logout()。Wait();
Environment.Exit(0);
}

静态异步System.Threading.Tasks.Task login()
{
try
{
loginResponse liResp = await SfLogin( );
lir = liResp.result;
}
catch(例外e)
{
Console.WriteLine("发生了意外错误:" + e.Message);
Console.WriteLine(e.StackTrace);
Environment.Exit(1);
}

if(lir.passwordExpired)
{
Console.WriteLine("发生错误。您的密码已过期。");
success = false;
}

endpoint = new EndpointAddress(lir.serverUrl);
header = new SessionHeader();
header.sessionId = lir.sessionId;

sc = new SoapClient(SoapClient.EndpointConfiguration.Soap,endpoint);
success = true;
}

静态异步System.Threading.Tasks.Task< loginResponse> SfLogin()
{
loginResponse liResp = await loginSc.loginAsync(null,null,userName,password + securityToken);
return liResp;
}

静态异步System.Threading.Tasks.Task logout()
{
try
{
logoutResponse loResp = await SfLogout( );
Console.WriteLine(" Logged out。);;
}
catch(例外e)
{
Console.WriteLine("发生了意外错误:" + e.Message);
Console.WriteLine(e.StackTrace);
Environment.Exit(1);
}
}

静态异步System.Threading.Tasks.Task< logoutResponse> SfLogout()
{
logoutResponse loResp = await sc.logoutAsync(header,null);
返回loResp;
}

}
}



Hello, 

I got provided external webservice wsdl url. 
The webservice is written in php. 
With every request I must send my credentials. 

 <soapenv:Header>
      <AuthHeader>
         <login>login</login>
         <password>password</password>
      </AuthHeader>
</soapenv:Header>



I sow example with using SOAPHEADERS according to this link
https://msdn.microsoft.com/en-us/library/77hkfhh8(VS.71).aspx
and have created all is needed. 
Credentials are assinged to service class but However it is not working.

I am not so good with PHP so I am wondering :
Is it possible that on providers PHP code there is not defined [SoapHeader("AuthHeader")] above methods ? 

I have added this header in generated code (before methods) after importing wsdl.

Do you have any suggestion?




EDIT: 
I have found the solution. The problem was in Naming of these field for Credentials. 
Provider of webservice did not defined proper Header Name. 
After renaming the fields it worked perfectly. 

Thank you very much for your time and replies. 


解决方案

Not sure if this helps.  I just created a webservice SOAP connection using .Net Core 2.0.  It connects to Salesforce using a WSDL.  Here is a sample.  It uses async methods.

using System;
using SFDC;
using System.ServiceModel;
using System.Threading.Tasks;
using System.IO;
using System.Collections.Generic;

namespace SfLogin
{
    class Program
    {
        static string userName = "";
        static string password = "";
        static string securityToken = "";
        static SoapClient loginSc;
        static SoapClient sc;
        static LoginResult lir;
        static SessionHeader header;
        static EndpointAddress endpoint;
        static bool success;

        static void Main(string[] args)
        {
            loginSc = new SoapClient();
            sc = new SoapClient();
            login().Wait();
            logout().Wait();
            Environment.Exit(0);
        }

        static async System.Threading.Tasks.Task login()
        {
            try
            {
                loginResponse liResp = await SfLogin();
                lir = liResp.result;
            }
            catch (Exception e)
            {
                Console.WriteLine("An unexpected error has occured: " + e.Message);
                Console.WriteLine(e.StackTrace);
                Environment.Exit(1);
            }

            if (lir.passwordExpired)
            {
                Console.WriteLine("An error has occurred. Your password has expired.");
                success = false;
            }

            endpoint = new EndpointAddress(lir.serverUrl);
            header = new SessionHeader();
            header.sessionId = lir.sessionId;

            sc = new SoapClient(SoapClient.EndpointConfiguration.Soap, endpoint);
            success = true;
        }

        static async System.Threading.Tasks.Task<loginResponse> SfLogin()
        {
            loginResponse liResp = await loginSc.loginAsync(null, null, userName, password + securityToken);
            return liResp;
        }

        static async System.Threading.Tasks.Task logout()
        {
            try
            {
                logoutResponse loResp = await SfLogout();
                Console.WriteLine("Logged out.");
            }
            catch (Exception e)
            {
                Console.WriteLine("An unexpected error has occured: " + e.Message);
                Console.WriteLine(e.StackTrace);
                Environment.Exit(1);
            }
        }

        static async System.Threading.Tasks.Task<logoutResponse> SfLogout()
        {
            logoutResponse loResp = await sc.logoutAsync(header, null);
            return loResp;
        }

    }
}


这篇关于从C#调用PHP webservice并在SOAPHeader中传递凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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