Register Header in a SOAP service reference in C#

Asked

Viewed 147 times

0

I’d like some support. I’m in serious trouble with a client. I have a C# application that fetches data from a system and sends it to a SOAP service. It turns out that this SOAP service requires a header:

<soapenv:Header>
 <wsAutenticacao>
    <login>us1</login>
    <senha>b81476fc88f1a2a4</senha>
    <codigoEmpresa>1</codigoEmpresa>
 </wsAutenticacao>
</soapenv:Header>

When mapping through Visual Studio, I can’t find where to insert header data. The solution was not initiated by me and I need to continue in the same pattern.

Services are mapped in the app.config as Properties.Settings and not as Binding:

 <setting name="wsSGOSituacaoProjeto_SituacaoProjetoWSImplService"
                serializeAs="String">
   <value>http://ser:8080/erp/ws/situacaoProjeto</value>
 </setting>

The call in the reference contains nothing about the header:

public ProjetoWSImplService() {
            this.Url = global::Integracao.SGO.Properties.Settings.Default.Projeto_ProjetoWSImplService;
            if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
                this.UseDefaultCredentials = true;
                this.useDefaultCredentialsSetExplicitly = false;
            }
            else {
                this.useDefaultCredentialsSetExplicitly = true;
            }
        }

        public new string Url {
            get {
                return base.Url;
            }
            set {
                if ((((this.IsLocalFileSystemWebService(base.Url) == true) 
                            && (this.useDefaultCredentialsSetExplicitly == false)) 
                            && (this.IsLocalFileSystemWebService(value) == false))) {
                    base.UseDefaultCredentials = false;
                }
                base.Url = value;
            }
        }

        public new bool UseDefaultCredentials {
            get {
                return base.UseDefaultCredentials;
            }
            set {
                base.UseDefaultCredentials = value;
                this.useDefaultCredentialsSetExplicitly = true;
            }
        }

        /// <remarks/>
        public event consultarProjetoCompletedEventHandler consultarProjetoCompleted;

        /// <remarks/>
        public event inserirCompletedEventHandler inserirCompleted;

        /// <remarks/>
        public event consultarCompletedEventHandler consultarCompleted;

        /// <remarks/>
        public event alterarCompletedEventHandler alterarCompleted;

The part of the code that is error due to missing header is as follows:

if (WebServiceSGO == null)
            {
                Iniciar("http://ser:8080/erp/ws/projeto?wsdl");
                //WebServiceSGO = new Integracao.SGO.wsSGOProjeto.ProjetoWSImplService();
            }
            WebClient client = new WebClient();
            var ret = WebServiceSGO.consultar(Param.Empresa, Param.EmpresaEspecificada, Param.UnidadeAdministrativa, Param.Datainicio, Param.DatainicioEspecificada, Param.Projeto, Param.AnoProjeto, Param.AnoProjetoEspecificado, Param.TipoProjeto, Param.Cliente, Param.ClienteEspecificado);
            return ToXML(ret);

Return: "Login Failure!"

  • where you get the login/password/password data?

  • It was provided to me by the company that built the service. It’s statistical information. <soapenv:Header> <wsAutenticacao><login>Erp</login><password>b814720948b6bf65e0b5a6fc88f1a2a4</password><codigoEmpresa>1</codigoEmpresa></wsAutenticacao> </soapenv:Header>

  • wanted to say if it is in a configuration file for example? but in your case I think it will not help, I will put an answer with an example

1 answer

1

You can set the header like this:

WebClient client = new WebClient();
client.Headers.Add("login", "us1");
client.Headers.Add("senha", "b81476fc88f1a2a4");
client.Headers.Add("codigoEmpresa", "1");
  • It’s a ready-made project. As it was done, the reference does not let do this operation comes as an interface (I will try to show here): public Projectowsimplservice() { this.Url = global::Integration.SGO.Properties.settings.Default.Georede_sgo_wssgoprojeto_projectowsimplservice; if ((this.Islocalfilesystemwebservice(this.Url) == true)) { this.Usedefaultcredentials = true; this.useDefaultCredentialsSetExplicitly = false; } Else { this.useDefaultCredentialsSetExplicitly = true; } } ...

  • another part of the code and reference: if (Webservicesgo == null) Webservicesgo = new Integration.SGO.wsSGOProject.Projectsimplservice(); reference: public Projectowsimplservice() { this. Url = global::Integration.SGO.Properties.settings.Default.Georede_sgo_wssgoprojeto_projectowsimplservice; if ((this.Islocalfilesystemwebservice(this.Url) == true) { this.Usedefaultcredentials = true; this.useDefaultCredentialsSetExplicitly = false; ...

  • 1

    but if the code is ready, as you will add the header? need to edit the code

  • And that the service in which we are communicating passed require headset with credentials.

  • Actually I meant, the project with source code came ready. I have the source code with me.

  • but these codes you’ve put are a little different than what’s in the question, which simply creates a WebClient... but come on, the proxy you use to consume the service is "Webservicesgo" right? How do you create it? There is something on your Web.config, such as the service address, or you create it by passing the address?

  • Do you have an email? I’ll give you the source.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.