I have a weird issue with my Windows Phone 8 app. I’m trying to consume a Web API service that’s hosted in the cloud, but I keep getting 404 errors every time I make the request.
The strange thing is that the exact same service works perfectly when I call it from my Windows 8 desktop application. So I know the API endpoint is working fine.
Here’s the code I’m using in my Windows Phone app:
WebClient httpClient = new WebClient();
httpClient.DownloadStringCompleted += OnDownloadComplete;
httpClient.DownloadStringAsync(new Uri("http://myserviceapi.cloudapp.net/api/products?filter=active", UriKind.Absolute));
void OnDownloadComplete(object sender, DownloadStringCompletedEventArgs args)
{
string response = args.Result;
}
I’ve also tried using HttpWebRequest and HttpWebResponse classes but got the same 404 response. The service was also giving me trouble when running locally, but now that it’s deployed to the cloud it should work without any network restrictions.
Has anyone run into similar issues with Windows Phone 8 and Web API calls? What could be causing this platform-specific problem?
Had this exact problem before - it’s usually a user agent issue. Windows Phone 8’s WebClient sends a different user agent than desktop apps, and some cloud hosts or load balancers block requests from mobile user agents they don’t recognize. Set a custom user agent header to fake a desktop browser: httpClient.Headers[HttpRequestHeader.UserAgent] = “Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1”. Also worth checking if your cloud service has mobile-specific routing or a separate mobile endpoint you should use instead.
Add the content-type header explicitly - wp8 gets picky about that. Also check if your cloud service does device detection based on request headers. I had the same weird issue where the api worked fine from Postman and desktop but failed on wp8. Turned out the server expected an accept header that was missing.
Check your Windows Phone 8 emulator network settings first. I hit this same issue last year - turns out the emulator routes requests through a different network setup that doesn’t handle SSL certs properly, even with HTTP URLs. Your cloud service might be forcing HTTPS redirects that the phone handles differently than desktop. Try hitting the API directly from the phone browser to see if you get the same 404. Also make sure your app has the right network capabilities in WMAppManifest.xml - you’ll need ID_CAP_NETWORKING and ID_CAP_WEBBROWSERCOMPONENT for complex HTTP stuff.
This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.