locked
HttpClient onWindows 8.1 Store app doesn't return a response

    Question

  • I'm developing a Windows 8.1 Store app with .NET Framework 4.5.1.

    I'm trying to connect to my own ASP.NET Web Api 2.2 Restful service. I have test it with PostMan an it works perfectly.

    I'm doing a GET with the following code:

       using Windows.Web.Http;
        
        public async Task<string> GET(string url)
        {
        	Uri resourceUri;
        	if (!Uri.TryCreate(url.Trim(), UriKind.Absolute, out resourceUri))
        	{
        		//statusText.Text = "Invalid URI, please re-enter a valid URI";
        		return null;
        	}
        	if (resourceUri.Scheme != "http" && resourceUri.Scheme != "https")
        	{
        		//statusText.Text = "Only 'http' and 'https' schemes supported. Please re-enter URI";
        		return null;
        	}
        
        	_customError.Reset();
        
        	HttpClient request = new HttpClient();
        	HttpResponseMessage response = new HttpResponseMessage();
        
        	var headers = request.DefaultRequestHeaders;
        
        	// Try to add user agent to headers.
        	if (headers.UserAgent.TryParseAdd(_userAgent))
        		headers.UserAgent.ParseAdd(_userAgent);
        			
        	try
        	{
        		//response = await request.GetAsync(resourceUri);
        
        		//response.EnsureSuccessStatusCode();
        
        		//return await response.Content.ReadAsStringAsync();
        		return await request.GetStringAsync(resourceUri);
        	}
        	catch (WebException ex)
        	{
        		WebResponse errorResponse = ex.Response;
        		if (errorResponse != null)
        		{
        			using (Stream responseStream = errorResponse.GetResponseStream())
        			{
        				StreamReader reader =
        					new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
        				String errorText = reader.ReadToEnd();
        
        				_customError = JsonConvert.DeserializeObject<CustomHttpErrorMessage>(errorText);
        			}
        		}
        		throw;
        	}
        }

    I have debugged both, client and Web Api, and Windows client reach Web Api, and Web Api controller returns NotFound(), but the executions doesn't continue on Windows client.

    This is the first time I develop a Windows 8.1 and the first time I use HttpClient and I don't know why I don't get any response from Web Api.

    Do you what's going on?


    Thursday, October 23, 2014 7:34 PM

Answers

  • Yes, exactly, localhost is only for test purpose. You could find more information from here: How to enable loopback and troubleshoot network isolation

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    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, October 24, 2014 8:01 AM
    Moderator

All replies

  • Hi VansFannel,

    First of all, I would like to know if your uri in the code is kind of localhost address, in Windows Store App we cannot communicate with localhost.

    Anyway I tested with your code, looks correct, I use the current forum thread link and I can get back a string which contains all html.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    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, October 24, 2014 6:00 AM
    Moderator
  • Hi Jamles,

    Yes, Web Api is running in an IIS Express and the URI is http://localhost:54333/... I'm debugging both, Web Api and Windows Store client. In other words, I have to Visual Studio 2013 Update 3 running, one with Web Api, and the other one with Windows Store app.

    Reading more question, I have found this answer:

    if you open the project properties

    go to debug, there you have an option allow local network loopback which have to be enabled.

    Now, I can't test it, but I will check it as soon I come back home.

    I also have found this sample, https://code.msdn.microsoft.com/windowsapps/httpclient-sample-55700664/view/SourceCode, to learn more about HttpClient.

    Thanks



    • Edited by VansFannel Friday, October 24, 2014 7:02 AM More details
    Friday, October 24, 2014 6:48 AM
  • Yes, exactly, localhost is only for test purpose. You could find more information from here: How to enable loopback and troubleshoot network isolation

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    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, October 24, 2014 8:01 AM
    Moderator