locked
[UWP]Network issue with HttpClient RRS feed

  • Question

  • Hi,

    in my Windows Universal App running on Windows 10 desktop, app uses Windows.Web.Http.HttpClient as below

    using (HttpClient client = new HttpClient()){
       var responseString = await client.GetStringAsync(new Uri("http://remoteserver.com/file.json"));
    }

    "remoteserver.com/file.json" is a mock, i'm using a concrete webservice with json response.

    I've, also, done test with different websites, but the debugger stops on "await client.GetStringAsync" without error and the apps is idle.

    In my Package.appxmanifest i've added "Internet (client)" capabilities.

    I've created a different version with CancellationTask as below

    try
    {
       CancellationTokenSource source = new CancellationTokenSource(2000); // 2 seconds
       HttpClient client = new HttpClient();
       var response = await client.GetStringAsync(new Uri("http://remoteserver.com/file.json")).AsTask(source.Token);
    }
    catch (TaskCanceledException ex)
    {
       // Catch operation aborted ...
    }

    This snipped is necessary to handle timeout, but it doesn't works, VisualStudio debugger blocks on "await client.GetStringAsync".

    Where did i wrong?

    I've tested in debug mode with Local Machine.

    Thank you.


    • Edited by Krunal Parekh Friday, January 1, 2016 9:18 AM subject
    Thursday, December 31, 2015 11:54 AM

Answers

  • Hello agumiro,

    You can do this on App Launched.

    Please see: App lifecycle

    The OnLaunched method is called when an app is launched including when the app is currently suspended in memory. The LaunchActivatedEventArgs parameter contains the previous state of your app and the activation arguments.

    With Regards,

    Krunal Parekh 


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Krunal Parekh Tuesday, January 5, 2016 7:00 AM
    • Marked as answer by agrumiro Tuesday, January 5, 2016 7:34 AM
    Friday, January 1, 2016 9:07 AM

All replies

  • Hello agrumiro,

    I tested below code and it works fine for me.

    		private async void Page_Loaded(object sender, RoutedEventArgs e)
    		{
    			var result = await GetAsync("http://www.w3schools.com/json/myTutorials.js");
    		}
    
    		public async Task<string> GetAsync(string uri)
    		{
    			var httpClient = new HttpClient();
    			var content = await httpClient.GetStringAsync(new Uri(uri));
    			return content;
    		}

    I suspect something wrong on the server side or network issue. Check this your network jsonfile using fiddler to see what the server response status is.

    With Regards,

    Krunal Parekh


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, January 1, 2016 5:37 AM
  • Hi, I' solved moving http request outside from page constructor. So now app starts correctly and downloads data after user pushs refresh button. How I can load network data on app starts? Thank you
    • Proposed as answer by Krunal Parekh Tuesday, January 5, 2016 7:00 AM
    Friday, January 1, 2016 8:55 AM
  • Hello agumiro,

    You can do this on App Launched.

    Please see: App lifecycle

    The OnLaunched method is called when an app is launched including when the app is currently suspended in memory. The LaunchActivatedEventArgs parameter contains the previous state of your app and the activation arguments.

    With Regards,

    Krunal Parekh 


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Krunal Parekh Tuesday, January 5, 2016 7:00 AM
    • Marked as answer by agrumiro Tuesday, January 5, 2016 7:34 AM
    Friday, January 1, 2016 9:07 AM
  • Hello agumiro,

    You can do this on App Launched.

    Please see: App lifecycle

    The OnLaunched method is called when an app is launched including when the app is currently suspended in memory. The LaunchActivatedEventArgs parameter contains the previous state of your app and the activation arguments.

    With Regards,

    Krunal Parekh 


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Ok, 

    thank you

    Tuesday, January 5, 2016 7:35 AM