无法使用C#或VB检索Web服务响应

时间:2013-04-19 20:54:19

标签: c# vb.net visual-studio-2010 web-services soap

我必须创建一个应用程序,将一些数据发送到(SOAP)WebService并显示其响应。这似乎很简单,但问题是在使用WS之后,变量仍然是空的。我没有运气就试过C#和VB。

这是代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            CardifWS.RequestContactWsService webService = new CardifWS.RequestContactWsService();

            string usuario = "user987";
            string name = "teste2003 1552";
            string cpf = "123456789";
            string email = "email@gmail.com";
            string zipCode = "04536010";
            string city = "sao paulo";
            string neighborhood = "itaim";
            string state = "sp";
            string address = "itaim";
            string addressNumber = "98";
            string addressComp = "2";
            int dddPhone = 11;
            string phone = "12345678";
            int dddCellPhone = 11;
            string cellPhone = "1187654321";
            int fipeCode = 123456;
            string chassi = "123456789";  
            string renavam = "987654321";
            string placa = "abc3456";
            string marca = "ford";
            string modelo = "ka";
            int colorCode = 456;
            int anoFabricacao = 2010;
            int anoModelo = 2011;
            int indice = 1;
            string[] parametros = "7".Split(' ');
            string bestTimeCall = "T";

            CardifWS.RequestContactResponse respuesta = webService.requestContact(usuario, name, cpf, email, zipCode, city,
                                                                                neighborhood, state, address, addressNumber,
                                                                                addressComp, dddPhone, phone, dddCellPhone,
                                                                                cellPhone, fipeCode, chassi, renavam, placa,
                                                                                marca, modelo, colorCode, anoFabricacao,
                                                                                anoModelo, indice, parametros, bestTimeCall);

            Console.WriteLine("Response: {0}", respuesta.ToString());
            Console.WriteLine("errorCode: {0}", respuesta.errorCode.ToString());
            Console.WriteLine("errorDesc: {0}", respuesta.errorDesc);
            Console.WriteLine("id: {0}", respuesta.id.ToString());
            Console.ReadLine();
        }
    }
}

这是WS URL

http://directsales2.uat.cardif.com.br:8080/autofacil-ws/services/RequestContactWs?wsdl

使用WireShark我肯定知道WS正在向我发送响应,但有些原因.NET似乎无法读取它。

我正在使用Visual Studio 2010

1 个答案:

答案 0 :(得分:1)

我检查了服务,显然响应的命名空间与服务命名空间不同。

  • 回复:Namespace =“http://response.autofacil.cardif.com.br
  • 服务:Namespace =“http://webservice.autofacil.cardif.com.br

我使用svcutil.exe为webservice生成代理类

SvcUtil.exe /o:c:\temp\foo.cs http://directsales2.uat.cardif.com.br:8080/autofacil-ws/services/RequestContactWs?wsdl

将类导入项目,并将服务添加到app.config

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="RequestContactWsSoapBinding" />
    </basicHttpBinding>
  </bindings>
  <client>
    <endpoint address="http://directsales2.uat.cardif.com.br:8080/autofacil-ws/services/RequestContactWs"
        binding="basicHttpBinding" bindingConfiguration="RequestContactWsSoapBinding"
        contract="Cardif.Autofacil.WebService.RequestContactWs" name="RequestContactWs" />
  </client>
</system.serviceModel>

之后,您可以在导入的类

中更改RequestContactResponse的命名空间
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace ="http://webservice.autofacil.cardif.com.br")]
public partial class RequestContactResponse