Catch data from WebService in Action from C #

2

I have an application that accesses third-party WebService. To improve the testing process, I'm setting up a Web site to simulate the WebService.

This site consists of several Actions, which return the XML, simulating data. This part is ok .

My problem is that I can not get the input parameters, which come from my application.

Remembering that my application continues with the same structure to send the data to the WebService, but only with the URL pointing to my site.

I tried to use Request in the controller, and I could not get the data.

Reference generated by C # when importing WSDL

[System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace="http://services.teste.com.br", ResponseNamespace="http://services.teste.com.br", Use=System.Web.Services.Description.SoapBindingUse.Literal)]
[return: System.Xml.Serialization.XmlElementAttribute("result")]
public moedasExportarOut Exportar(string user, string password, int encryption, moedasExportarIn parameters) {
    object[] results = this.Invoke("Exportar", new object[] {
                user,
                password,
                encryption,
                parameters});
    return ((moedasExportarOut)(results[0]));
}

Action

[HttpPost]
public ActionResult XML(string user, string password, int? encryption, string methodName,
    Moeda.moedasExportarIn parameters, 
    Moeda.moedasExportarIn arg, int? codEmpField)
{
    XmlDocument xd = new XmlDocument();
    var teste = "";
    teste =
        "<?xml version='1.0' encoding='UTF-8'?> " +
        "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"> " +
        "   <S:Body>" +
        "       <ns2:ExportarResponse xmlns:ns2=\"http://services.teste.com.br\">" +
        "           <result>" +
        "               <erroExecucao xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/>" +
        "               <finalizaramRegistros>S</finalizaramRegistros>" +
        "               <mensagemRetorno xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/>" +
        "               <numLot>0</numLot>" +
        "               <tipoRetorno>1</tipoRetorno>" +
        "           </result>" +
        "       </ns2:ExportarResponse>" +
        "   </S:Body>" +
        "</S:Envelope>";

    xd.LoadXml(teste);
    return new XmlResult(xd);
}

Calling the WebService, made by my application

public Teste.Moedas.moedasExportarOut ExportarMoeda(DadosParaIntegracao dados, Teste.Moedas.moedasExportarIn parameters)
{
    System.Net.ServicePointManager.DefaultConnectionLimit = conexaoSimultaneaPorServico;
    var service = new Teste.Moedas.g5services();
    service.Url = dados.Configuracao.URL;
    service.Timeout = ConfiguarTimeOut(service, dados.Configuracao);
    ConfigurarProxy(service, dados.Proxy);

    try
    {
        ConfigurarIntegracao(service);
        System.Net.ServicePointManager.Expect100Continue = false;
        return service.Exportar(dados.Login.Login, dados.Login.Senha, 0, parameters);
    }
    finally
    {
        LimparIntegracao();
    }
}

I could not even get the value of user, password or any other parameter.

    
asked by anonymous 26.06.2015 / 20:42

1 answer

3

From what I researched, the correct term for what I wanted was mock , which is something to simulate a WebService. My problem was getting what my WebServer client sends to the server. Searching the internet I found this:

var parametros = new StreamReader(Request.InputStream).ReadToEnd();

No Request.InputStream had all the request. So I added other features, and I was able to read the sent data, and respond to an XML, and to synapse the WebService.

Thanks for the personal help.

My full source is:

public class WSController : Controller
{

    private string RetornaPorta(string parametros)
    {
        try
        {
            var xml = new Regex("<soap:Body>(.*)</soap:Body>").Split(parametros)[1];
            return xml.Split(' ')[0].Substring(1);
        }
        catch
        {
            return "Erro na porta";
        }
    }

    private string RetornaChave(string parametros, ServicoWS servico)
    {
        if (String.IsNullOrEmpty(servico.Chave))
        {
            return "";
        }

        var retorno = "";
        foreach (var item in servico.Chave.Split(';'))
        {
            try
            {
                var valor = new Regex(String.Format("<{0}>(.*)</{0}>", item)).Split(parametros)[1];
                retorno += item + "=" + valor + ";";
            }
            catch
            {

            }
        }
        return retorno.Substring(0, retorno.Length - 1);
    }

    [WebMethod]
    public ActionResult ConfirmarResposta()
    {
        var parametros = new StreamReader(Request.InputStream).ReadToEnd();
        var servico = new ServicoWS();

        using (var db = new Conexao())
        {
            servico = db.ServicoWS.BuscaTipoEPorta("ConfirmarResposta", RetornaPorta(parametros));
            var chave = RetornaChave(parametros, servico);

            var xml = db.ServicoWSRetorno.Where(w =>
                w.ServicoWSID == servico.ServicoWSID &&
                w.Chave == chave).FirstOrDefault();

            XmlDocument xd = new XmlDocument();
            xd.LoadXml(xml.Retorno);
            return new XmlResult(xd);
        }
    }
}
    
30.06.2015 / 23:53