0
  1. I have webapi and method:

    public class ArticlesController : ApiController
    {
         //
         // GET: api/values
         public IEnumerable<Article> Get()
         {
             using (CollectionsEntities entities = new CollectionsEntities())
             {
                  return entities.Articles.ToList<Article>();
             }
         }
     }
    

It works fine, and check it in browser:

webapi works

  1. I create a client:

    public static class CollectionClient
    {
        private static readonly HttpClient client;
    
        public static Uri ServerBaseUri
        {
            get { return new Uri("http://localhost:10779/api/"); }
        }
    
        public static Boolean IsDirty { get; private set; }
    
        static CollectionClient()
        {
            IsDirty = true;
            client = new HttpClient(new DemoHttpMessageHandler());
        }
    
        // get all articles
        public static async Task<List<Article>> GetAllArticlesAsync()
        {
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            Uri serverUril = new Uri(ServerBaseUri, "Articles");            
            var response = await client.GetAsync(serverUril);
    
            response.EnsureSuccessStatusCode();
    
            var articles = await response.Content.ReadAsAsync<List<Article>>();
            IsDirty = false;
            return articles;   
        }
    }
    

The response gets 404 status code: enter image description here

ID_CAP_NETWORKING is checked to make sure it can access internet. Could anybody give me a hand? Thanks in advance!

mehdi lotfi
  • 11,194
  • 18
  • 82
  • 128
max
  • 645
  • 3
  • 11
  • 22

2 Answers2

1

I only have experience with Android but "localhost" for the emulator is probably different than the "localhost" for your PC. Try using the internal subnet IP instead.

Dmitry S.
  • 8,373
  • 2
  • 39
  • 49
  • i use 127.0.0.1 instead, still the same, any ideas? – max Dec 21 '13 at 02:45
  • 127.0.0.1 is the same thing as the localhost (a loopback address). You need to use something like 192.168.1.x or whatever the local subnet is. – Dmitry S. Dec 21 '13 at 02:47
  • 2
    Here is a very similar question: http://stackoverflow.com/questions/13149304/windows-phone-8-emulator-access-localhost that has a detailed answer. – Dmitry S. Dec 21 '13 at 02:48
  • i use a public api to try, it works. that proves program is collect, will publish the web api and re-try. thnx. – max Dec 23 '13 at 08:00
0

In windows phone 7.x, the emulator shared the networking of the Host PC, that means: we could host services on our PC and access them from our code using htt://localhost..

In windows phone 8, the emulator is a virtual machine running under Hyper V, that means: you cannot access services on your PC using htt://localhost..

you must use the correct host name or raw IP address of our host PC in URIs

mehdi lotfi
  • 11,194
  • 18
  • 82
  • 128
max
  • 645
  • 3
  • 11
  • 22