Is it possible to access an Api Web by Windows Phone?

1

I developed an Api that communicates with my database and put it in IIS. Testing in Windows Forms applications, api data search works normally, but when I try to run it inside the Windows Phone app it returns the following error:

  

{System.Net.WebException: The remote server returned an error: NotFound. --- > System.Net.WebException: The remote server returned an error: NotFound. at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse (IAsyncResult asyncResult) at System.Net.Browser.ClientHttpWebRequest.

asked by anonymous 16.10.2015 / 21:35

2 answers

0

This NotFound error is a much more general error than you think. It can occur when the API does not actually exist, or exists but returns the incorrect status code or, what I find more common, when it gives some error due to proxy.

I have applications in the WindowsPhone store that consume WebApi. Once you're sure it's not proxy problem, firewall, etc., the code below works perfectly in my application.

Note:

1 - My app is Windows Phone Silverlight 8.1.

2 - Whenever possible test your Apps right on your device

try
{
    var webClient = new WebClient();

    // A minha WebApi implementa um DelegatingHandler para autenticação customizada
    // Por isso passo o usuario e senha.
    // Se a sua nao tiver, basta comentar a linha abaixo.
    webClient.Credentials = new NetworkCredential("Usuario", "Senha");

    // Progresso do download do arquivo
    webClient.DownloadProgressChanged += (sender, e) =>
    {
        Dispatcher.BeginInvoke(() =>
        {
            // Label com o total de bytes ja baixado. Tipo: 200 de 5000, 300 de 5000, etc...
            lblDownloadStatus.Text = "Baixado " + GetFileSize(e.BytesReceived) + "/" + GetFileSize(e.TotalBytesToReceive);

            // Percentual baixado. Tipo: 5%... 10%... etc...
            lblPercentual.Text = e.ProgressPercentage + "%";
        });
    };

    // Fim do processo de download do arquivo. Agora é só tratar o que voce quiser.
    webClient.OpenReadCompleted += (sender, e) =>
    {
        if (e.Error == null && !e.Cancelled)
        {
            Dispatcher.BeginInvoke(() =>
            {
                Minha_Funcao_Para_Tratar_O_Stream_Baixado(e.Result);
            });
        }
        else
        {
            string erro = e.Error.Message;
            if (erro.ToString().Contains("not found"))
            {
                MessageBox.Show("Não foi possível realizar o download do arquivo. Por favor, verifique as configurações da sua internet e tente novamente.");
            }
            else
            {
                MessageBox.Show("Erro ao baixar arquivo: " + erro);
            }
        }
    };

    // Abre e executa a URL.
    webClient.OpenReadAsync(new Uri("http://www.suaapi.com/DownloadArquivo"));
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

Hugs.

    
15.01.2016 / 12:12
0

Are you using localhost? You can not even test the emulator. You need to swap localhost for machine IP.

    
20.02.2016 / 10:07