Consume Webservice WSDL

Asked

Viewed 2,827 times

0

Good afternoon,

I am trying to consume a Webservice using ASP.NET, however, after adding its reference and executing the method, the parameters return null, generating Exception. I tried to run 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?

  • tried to add as Service Reference?

  • Yes, I tried as Service Reference and sending XML with Webrequest. If you want, you can try to run, I left the parameters in the code.

  • gave problem enconding XML, the same was using iso-8859-1. You can see the following answer: http://stackoverflow.com/questions/1908030/calling-a-webservice-that-uses-iso-8859-1-encoding-from-wcf

  • He changed the format to UTF-8, but now the return is null. Using SOAPUI gets the return normally, which can be?

1 answer

1


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

I just tested this web-service. To do so I added the same through a Web Reference and I called it 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 has the sole purpose of inspecting the messages being sent and received. It follows below its 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;
    }
}

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 received answer:

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 believe the WCF Client is having trouble deserializing the message, so it is 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 - Expected Response

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

<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 set up a WCF Service that mimics what you are trying to access, it returned me the Token and Validadetoken without problems, I will put down the sending and receiving messages only for comparison purposes:

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>
  • I managed to simulate here, it is possible to recover the returned xml in the behavior and treat it to capture only the return parameters with xmlNode for example?

  • You can manipulate the return message in the method . Afterreceivereply() of Messageinspector, for this just manipulate the reply object.

  • for this you can do the following: reply = Message.Createmessage(reply. Version, null, xmlReader);

  • And the moment I call the Gettoken method (token = client.Gettoken(1234567890, 2, "1.0", out validity, out error);) How I capture this information?

  • As soon as I have a moment, I’ll put a definitive answer.

Browser other questions tagged

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