Consume WebService WSDL

0

Good afternoon,

I am trying to consume a WebService using ASP.NET, however, after adding the reference of the same and executing the method, the parameters return null, generating exception. I tried running through WebRequest too, but returned the error below:

The remote server returned an error: (400) Bad Request.SystemSystem.Collections.ListDictionaryInternal

Code

        string ret = String.Empty;
        string validade;
        erro erro = new erro();

        CustomBinding binding = new CustomBinding(
        new CustomTextMessageBindingElement("utf-8", "text/xml", MessageVersion.Soap11),
        new HttpTransportBindingElement());

        var sr = new SoapUraClient();
        sr.Endpoint.Binding = binding;

PARAMETROS //cnpjParceiro = 1234567890;codigoParceiro= 2; versaoWS = 1.0


        var teste = sr.GetToken(Convert.ToInt32(cnpjParceiro), Convert.ToInt32(codigoParceiro), versaoWS, out validade, out erro);

        ret = validade + '|' + erro.codErro + '|' + erro.codErro;
        return validade;

Can you help me?

    
asked by anonymous 11.02.2015 / 17:48

1 answer

1

Note: This is not a response, but a detailed analysis of the request.

I just tested this web-service. For this I added the same through a Web Reference and I did it I called the same as follows:

var token = String.Empty;
var validade = String.Empty;
var erro = default(UraService.erro);
try
{                
    var cliente = new UraService.SoapUraClient();
    cliente.Endpoint.EndpointBehaviors.Add(new SimpleEndpointBehavior());
    token = cliente.GetToken(1234567890, 2, "1.0", out validade, out erro);
}
catch (Exception err)
{ 
    Debugger.Break();
}
Debugger.Break();

This Behavior is for the sole purpose of inspecting messages being sent and rectified. Here's your implementation:

SimpleEndpointBehavior

public class SimpleEndpointBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {

    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.MessageInspectors.Add(new SimpleMessageInspector());
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {

    }

    public void Validate(ServiceEndpoint endpoint)
    {

    }
}

SimpleMessageInspector

public class SimpleMessageInspector : IClientMessageInspector
{
    public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {
        var xml = reply.ToString();
        Debugger.Break();
    }

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
    {
        var xml = request.ToString();
        Debugger.Break();
        return null;
    }
}

The Request is being sent as follows:

SoapUraService - Request

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">get_token</Action>
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <GetToken xmlns="http://omnifacil3.omni.com.br/desenv">
      <cnpjParceiro xmlns="">1234567890</cnpjParceiro>
      <codigoParceiro xmlns="">2</codigoParceiro>
      <versaoWS xmlns="">1.0</versaoWS>
    </GetToken>
  </s:Body>
</s:Envelope>

And below the response received:

SoapUraService - Response

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" />
  <soap:Body>
<Response xmlns="http://omnifacil3.omni.com.br/desenv">
  <GetToken>
    <CodToken>499935661838159</CodToken>
    <ValidadeToken>12/02/2015 23:59:59</ValidadeToken>
  </GetToken></Response>
</soap:Body>
</soap:Envelope>

I think the WCF Client is having problems when it deserializes the message, so it's assigning null to token, validity and error.

If you look in the WSDL file, you will find the following snippets:

WSDL

<portType name="SoapUra">
    <operation name="GetToken">
        <input message="tns:GetToken"/>
        <output message="tns:GetTokenResponse"/>
    </operation>
    <operation name="GetDadosAgente">
        <input message="tns:GetDadosAgente"/>
        <output message="tns:GetDadosAgenteResponse"/>
    </operation>
</portType>
...
<xs:complexType name="GetToken">
    <xs:sequence>
        <xs:element name="cnpjParceiro" type="xs:int" minOccurs="1" maxOccurs="1"/>
        <xs:element name="codigoParceiro" type="xs:int" minOccurs="1" maxOccurs="1"/>
        <xs:element name="versaoWS" minOccurs="1" maxOccurs="1">...</xs:element>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="GetTokenResponse">
    <xs:sequence>
        <xs:element name="CodToken" minOccurs="0" maxOccurs="1">...</xs:element>
        <xs:element name="ValidadeToken" minOccurs="0" maxOccurs="1">...</xs:element>
        <xs:element name="erro" type="tns:erro" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
</xs:complexType>

SoapUraService - Response Expected

It should be noted that Response is in a format different from that expected by the Customer. Below is the Expected XML:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" />
    <soap:Body>
        <GetTokenResponse xmlns="http://omnifacil3.omni.com.br/desenv">
            <CodToken>499935661838159</CodToken>
            <ValidadeToken>12/02/2015 23:59:59</ValidadeToken>
        </GetTokenResponse>
    </soap:Body>
</soap:Envelope>

Finally, I mounted a WCF Service that imitates what you are trying to access, it returned the Token and ValidityToken without any problems, I will put down the sending and receiving messages for comparison purposes only:

WSTest - Request

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService1/GetToken</Action>
  </s:Header>
  <s:Body>
    <GetToken xmlns="http://tempuri.org/">
      <cnpjParceiro>1234567890</cnpjParceiro>
      <codigoParceiro>2</codigoParceiro>
      <versaoWS>1.0</versaoWS>
    </GetToken>
  </s:Body>
</s:Envelope>

WSTest - Response

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <GetTokenResponse xmlns="http://tempuri.org/">
      <GetTokenResult>499935661838159</GetTokenResult>
      <ValidadeToken>12/02/2015 23:59:59</ValidadeToken>
      <erro i:nil="true" xmlns:a="http://schemas.datacontract.org/2004/07/WcfService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
    </GetTokenResponse>
  </s:Body>
</s:Envelope>
    
12.02.2015 / 14:08