Consume Web Service in Java

3

I'm a beginner in C #, my Visual Studio is 2010, my Windows Phone version is 7. I searched the internet and in this version of the "async" command only Visual Studio 12 works up.

Do you have any other alternatives?

In a C # Desktop application it works without any problems.

I can map the web service with service references, but the return always comes null, but in Android works, what error can it be?

The error that appears is as follows: Unmarshalling Error: unexpected element (uri: "webservices.com.br/"; site: "parameter1"). The expected elements are < {} parameter2 & gt ;, < {} parameter1 >

Follow the source code:

public partial class MainPage : PhoneApplicationPage
{
    wsServico.cadastroTo cadastroTo = null; 

    public MainPage(){
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e){
        wsServico.TesteJavaWsClient ws = new wsServico.TesteJavaWsClient();
        ws.returnServicoAsync("A", "B");         
        ws.returnServicoCompleted += new EventHandler<wsServico.returnServicoCompletedEventArgs>(ws_returnServicoCompleted);


    }

    private void ws_returnServicoCompleted(object sender,
       wsServico.returnServicoCompletedEventArgs e){
           cadastroTo = e.Result;
           textBox1.Text = cadastroTo.cadastroId.ToString();               
    }

}
    
asked by anonymous 22.05.2016 / 00:53

1 answer

0

There should be a simpler solution, but it follows the source code:

private void btnSoap_Click(object sender, RoutedEventArgs e)
{
   getSoap();

}

public void getSoap()
    {

        HttpWebRequest spAuthReq = HttpWebRequest.Create("http://192.168.1.30:8080/test/TesteJavaEndPointService?wsdl") as HttpWebRequest;
        //spAuthReq.Headers["SOAPAction"] = "http://192.168.1.30:8080/test/TesteJavaEndPointService/returnServico";
        spAuthReq.ContentType = "text/xml; charset=utf-8";
        spAuthReq.Method = "POST";
        spAuthReq.BeginGetRequestStream(new AsyncCallback(selectedGetSoap), spAuthReq);
    }

    private void selectedGetSoap(IAsyncResult asyncResult)
    {
        string parametro1 = "C";
        string parametro2 = "D";

        string AuthEnvelope =
                    @"<?xml version=""1.0"" encoding=""utf-8""?>
                        <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:web=""http://localhost:8080/test"">
                        <soapenv:Header/>
                        <soapenv:Body>
                            <web:returnServico>
                                <parametro1>" + parametro1 + @"</parametro1>
                                <parametro2>" + parametro2 + @"</parametro2>
                            </web:returnServico>
                        </soapenv:Body>
                        </soapenv:Envelope>";

        UTF8Encoding encoding = new UTF8Encoding();
        HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
        System.Diagnostics.Debug.WriteLine("REquest is :" + request.Headers);
        Stream _body = request.EndGetRequestStream(asyncResult);
        string envelope = string.Format(AuthEnvelope);
        System.Diagnostics.Debug.WriteLine("Envelope is :" + envelope);
        byte[] formBytes = encoding.GetBytes(envelope);
        _body.Write(formBytes, 0, formBytes.Length);
        _body.Close();
        request.BeginGetResponse(new AsyncCallback(getAllGetSoapCallback), request);
    }

    private void getAllGetSoapCallback(IAsyncResult asyncResult)
    {

        try
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                System.Diagnostics.Debug.WriteLine("Async Result is :" + asyncResult);
                HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
                if (request != null && response != null)
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {

                        StreamReader reader = new StreamReader(response.GetResponseStream());
                        string Notificationdata = Util.RemoveAllNamespaces(reader.ReadToEnd());
                        XmlReader xmlReader = XmlReader.Create(new StringReader(Notificationdata));

                        cadastroTo = new wsServico.cadastroTo();

                        while (xmlReader.Read())
                        {
                            if (xmlReader.IsStartElement())
                            {
                                if (xmlReader.Name == "cadastroId")
                                {
                                    if (xmlReader.Read())
                                    {
                                        cadastroTo.cadastroId = Convert.ToInt64(xmlReader.Value.Trim());
                                    }
                                }

                                if (xmlReader.Name == "quantidade")
                                {
                                    if (xmlReader.Read())
                                    {
                                        cadastroTo.quantidade = Convert.ToInt32(xmlReader.Value.Trim());
                                    }
                                }
                            }
                        }
                        reader.Close();
                    }
                    StringBuilder output = new StringBuilder();
                    output.Append("Id:" + cadastroTo.cadastroId);
                    output.Append("\r\n");
                    output.Append("Quantidade:" + cadastroTo.quantidade);
                    output.Append("\r\n");

                    Dispatcher.BeginInvoke(() =>
                    {
                        txtSoap.Text = output.ToString(); 
                    }); 
                }
            }
            else
            {
                Dispatcher.BeginInvoke(() => MessageBox.Show("A rede nao esta disponivel"));
            }
        }
        catch (Exception ex)
        {
            Dispatcher.BeginInvoke(() => MessageBox.Show(ex.Message));
        }
    }
    
04.12.2016 / 20:55