none
Call PHP webservice from C# and pass credentials in SOAPHeader RRS feed

  • Question

  • 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. 


    • Edited by smiletic Monday, November 27, 2017 8:43 AM
    Thursday, November 23, 2017 1:26 PM

All 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;
            }
    
        }
    }

    Thursday, November 23, 2017 2:04 PM
  • Hi smiletic,

    According to your question is more related to PHP, you could post a thread to PHP forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools . If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Best Regards,

    Fei Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 24, 2017 7:06 AM
    Moderator