0

I have a problem with my universal app project (Universal Windows 8.1). I want to download a string which I then deserialize to a classes. But I encourage a problem with downloading this string on windows phone. I have a separate lib. Where I have only httpclient which downloads string and then deserialize it to classes. Everything works fine (tests are green), but when I plug it into windows phone app project. It looks like, it goes inside library code, but doesn't download anything.. I move code to download string to a windows phone solution to check why it doesn't work. But right now I'm constantly getting an exception like this:

System.Exception: Exception from HRESULT: 0x80072F30
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at ProjectName.Mobile.MainPage.<InitComboBoxes>d__10.MoveNext()

Here is my code:

private HttpClient client;
private CancellationTokenSource cts;
private HttpBaseProtocolFilter filter;

private async void Page_Loaded(object sender, RoutedEventArgs e)
{
    filter = new HttpBaseProtocolFilter();
    client = new HttpClient(filter);
    cts = new CancellationTokenSource();
    await InitComboBoxes();
}

private void Page_Unloaded(object sender, RoutedEventArgs e)
{
    filter?.Dispose();
    client?.Dispose();
    cts?.Dispose();
}

private async Task InitComboBoxes()
{
    try
    {
        var response = await client.GetAsync(new Uri(@"http://planer.info.pl/api/rest/places.json")).AsTask(cts.Token);
        var t = await response.Content.ReadAsStringAsync().AsTask(cts.Token);
    }
    catch (Exception e)
    {
        var msg = new MessageDialog("Please check Your internet connection!" + e.Message);
        await msg.ShowAsync();
    }
}

Right now I don't have any idea how to resolve this problem.. If anyone could help me I will be very grateful!

MNie
  • 1,347
  • 1
  • 15
  • 35

2 Answers2

1

Looking at your code, I don't see anything particularly wrong with it (and I didn't test it); except:

  • HttpResponseMessage is IDisposable so you should consider Disposing that also.

But, this is just some criticism :P

Looking at the Exception, according to MSDN the issue is that you cannot connect to the server.

ERROR_WINHTTP_NO_CM_CONNECTION

0x80072F30

There was a problem connecting to the server.

There are many ways that this issue can arrise:

  • Certificate
  • Firewall
  • Server is off

When working with Phone make sure that the Time on the phone is setup correctly (I've seen this causing a lot of issues)

it goes inside library code, but doesn't download anything..

I'm not sure what you mean by that, but if you mean it goes in and does not return then I suspect a threading deadlock.

Hope this helps.

Community
  • 1
  • 1
Barnstokkr
  • 2,904
  • 1
  • 19
  • 34
  • "HttpResponseMessage is IDisposable so you should consider Disposing that also." Good catch! I will check what You propose.. – MNie May 06 '16 at 05:58
  • Your answer target me to what is wrong. I forget to set up a internet connection on phone. This is why every tests passed, but I don't get response on phone... Many Thanks.. :) – MNie May 07 '16 at 11:39
0

If you only want to get data from url then try this code. Unless there is a reason this doesn't suit your needs?

    public async Task<string> GetData()
    {
        using (var client = new HttpClient())
        {
            try
            {
                HttpResponseMessage responseMessage = client.GetAsync("http://planer.info.pl/api/rest/places.json").Result;
                if (responseMessage.IsSuccessStatusCode)
                    return await responseMessage.Content.ReadAsStringAsync();
            }
            catch (Exception e)
            {
            }
        }
        return null;
    }

String response = await GetData();
Developer
  • 59
  • 7