locked
Get Data from API which is hosted on local Machine

    Question

  • I am using VS2013 to develop a databound app in win phone 8. I hosted a api on IIS Express on local machine with IP. When i browse the API on browser is comes easily,But a error is coming when trying to access from VS Solution.I am attaching my code and the error. the code is following

                         const string apiUrl = @"http://169.254.80.80/api/guest";

            public MainViewModel()
            {
                this.Items = new ObservableCollection<ItemViewModel>();
            }

            /// <summary>
            /// A collection for ItemViewModel objects.
            /// </summary>
            public ObservableCollection<ItemViewModel> Items { get; private set; }

            private string _sampleProperty = "Sample Runtime Property Value";
            /// <summary>
            /// Sample ViewModel property; this property is used in the view to display its value using a Binding
            /// </summary>
            /// <returns></returns>
            public string SampleProperty
            {
                get
                {
                    return _sampleProperty;
                }
                set
                {
                    if (value != _sampleProperty)
                    {
                        _sampleProperty = value;
                        NotifyPropertyChanged("SampleProperty");
                    }
                }
            }

            /// <summary>
            /// Sample property that returns a localized string
            /// </summary>
            public string LocalizedSampleProperty
            {
                get
                {
                    return AppResources.SampleProperty;
                }
            }

            public bool IsDataLoaded
            {
                get;
                private set;
            }

            /// <summary>
            /// Creates and adds a few ItemViewModel objects into the Items collection.
            /// </summary>
            public void LoadData()
            {
                if (this.IsDataLoaded == false)
                {
                    this.Items.Clear();
                    this.Items.Add(new ItemViewModel() { ID = "0", LineOne = "Please Wait...", LineTwo = "Please wait while the catalog is downloaded from the server.", LineThree = null });
                    WebClient webClient = new WebClient();
                    webClient.Headers["Accept"] = "application/json";

                    webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadCatalogCompleted);
                    webClient.DownloadStringAsync(new Uri(apiUrl));


                }
            }


            private void webClient_DownloadCatalogCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                try
                {
                    this.Items.Clear();
                    if (e.Result != null)
                    {
                        var books = JsonConvert.DeserializeObject<GuestDetail[]>(e.Result);
                        int id = 0;
                        foreach (GuestDetail book in books)
                        {
                            this.Items.Add(new ItemViewModel()
                            {
                                ID = (id++).ToString(),
                                LineOne = book.GuestName,
                                LineTwo = book.Nationality,
                                LineThree = book.Profession.Replace("\n", " ")
                            });
                        }
                        this.IsDataLoaded = true;
                    }
                }
                catch (Exception ex)
                {
                    this.Items.Add(new ItemViewModel()
                    {
                        ID = "0",
                        LineOne = "An Error Occurred",
                        LineTwo = String.Format("The following exception occured: {0}", ex.Message),
                        LineThree = String.Format("Additional inner exception information: {0}", ex.InnerException.Message)
                    });
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(String propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (null != handler)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }

    the error is ...

    {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.<>c__DisplayClasse.<EndGetResponse>b__d(Object sendState)
       at System.Net.Browser.AsyncHelper.<>c__DisplayClass1.<BeginOnUI>b__0(Object sendState)
       --- End of inner exception stack trace ---
       at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
       at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
       at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
       at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)}

    Can anyone help me please, I am stuck with it.

     
    Sunday, March 01, 2015 9:39 AM

Answers

  • try using your computer name instead of an ip adress
    • Marked as answer by Arif Rabbani Sunday, March 01, 2015 4:44 PM
    Sunday, March 01, 2015 4:09 PM

All replies

  • Did you set up your router to forward the request to your computer?  the ip you posted looks an ip your internet service provider gave you.

    Fyi you can get 10 free azure websites

    Sunday, March 01, 2015 1:57 PM
  • Thanks for your information. My IP Address is the IP of the Windows Phone Emulator Internal Switch. I also tried with my local IP(192.168.......) but same result.
    Sunday, March 01, 2015 3:30 PM
  • try using your computer name instead of an ip adress
    • Marked as answer by Arif Rabbani Sunday, March 01, 2015 4:44 PM
    Sunday, March 01, 2015 4:09 PM
  • Thanks it works with a little modification of disabling the antivirus protection.
    Sunday, March 01, 2015 4:44 PM