Consuming Java Web Service

Asked

Viewed 730 times

4

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

Is there any other alternative?

In a C# Desktop application works without any problem.

I can map the web service with the service References, but the return always comes null, but on Android works, which error can be ?

The error that appears is as follows: Unmarshalling Error: Unexpected element (Uri: "webservices.com.br/"; site: "parameter1"). The expected Elements are <{} parameter2>, <{} 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();               
    }

}
  • 1

    Cosengui solve the problem.

  • put as response to the next also know what you did

  • I put the solution on my personal website follow link: http://www.tidicas.com.br/? p=911

  • Great. If the site goes down, the solution reference is lost.

  • The idea is that the site no longer comes out.

  • Evaldojunior It would be nice if you put the solution here as @Washingtonmorais suggests. When you have a little time help us with your solution. Abs

Show 1 more comment

1 answer

0

There must 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));
        }
    }

Browser other questions tagged

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