4

I am now on Windows 8.1 and using VS 2012. I am using a Web api backend for all the heavy lifting in my Windows Phone app. However when I ran my app through the emulator all requests to my locally running web api where rejected.

I found that the emulator was unable to resolve localhost. After doing something very simulator in this article I was able to the Windows 8 emulator no problems.

However I need to now test it on my actual device to use the camera but I am running into the same problem as I did with the emulator. I can't communicate with my local web api.

I had no problems when I was on Windows 7 and on VS 2010. I just used the steps in the article and had no problems.

I get this error when I try to do a request to my web api from my device(the device is hooked in by usb through the computer)

<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\"http://www.w3.org/TR/html4/strict.dtd\">
<HTML>
  <HEAD>
    <TITLE>Bad Request</TITLE>
    <META HTTP-EQUIV=\"Content-Type\" Content=\"text/html; charset=us-ascii\">
  </HEAD>
  <BODY>
    <h2>Bad Request - Invalid Hostname</h2>
    <hr>
    <p>HTTP Error 400. The request hostname is invalid.</p>
  </BODY>
</HTML>

What do I need to setup to get this to work?

Edit

I still have this problem. I been able to get the Windows Phone 8 emulator to work but still can't get my WP7 device to be able to talk to a local Web Api.

Sample Code I will do something like this

public class CourseService
{
    public const string webApiUrl = "http://localhost:4372/api/values";
    RestClient client = new RestClient(webApiUrl);
    public void GetCourses(Action successCallback, Action<string> errorCallBack, bool forceError = false)
    {
        var request = new RestRequest(Method.GET);

        client.ExecuteAsync(request, response =>
        {
            if (forceError)
            {
                errorCallBack("Failed");
            }
            else
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    successCallback();
                }
                else
                {
                    errorCallBack("Failed");
                }
            }
        });
    }
}



 public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
    }
  • I tried localhost (knew it would not work from what I read)
  • tried my computer ip address
  • tried to put in IIS Express in the app config an ip address to use

    < bindings> < binding protocol="http" bindingInformation=":55210:localhost" /> < binding protocol="http" bindingInformation=":55210:169.254.80.80" /> < /bindings>

chobo2
  • 83,322
  • 195
  • 530
  • 832
  • for WP7 i will recommend to check http://stackoverflow.com/questions/10444273/httpclient-and-httpget-support-in-wp7 – Kamran Shahid Nov 18 '13 at 06:55
  • I don't have a problem with how to code it, I am having a problem for them to all talk locally. I had a fully functioning app that works. I am just missing something that would allow me to plug in my device to my computer, run a local asp.net web api copy and have the device and the web api talk to each other locally. – chobo2 Nov 18 '13 at 07:00
  • May be following will help http://stackoverflow.com/questions/16118558/work-with-web-api-and-windows-phone – Kamran Shahid Nov 18 '13 at 07:18
  • 1
    Try to disable the firewall of your local machine and try to connect via the IP address/TheUrlToConnectYourWebApi... Does that work? – pazcal Nov 20 '13 at 10:31
  • Ya I tried that disabled my anti virus and windows firewall. On my local machine using the ip address will work. I then copied that same url and tried on the phone through the browser but got 404. Then when I tried again I got bad request. – chobo2 Nov 20 '13 at 18:05
  • @chobo2 are you using your device and localhost on same network? – techloverr Nov 21 '13 at 10:27
  • @techloverr - what do you mean using on the same network? – chobo2 Nov 21 '13 at 17:47
  • @chobo2 means on same wifi or vpn – techloverr Nov 21 '13 at 18:06
  • My device is directly plugged into my computer which is running my web api. It works on the emulator(though it has stopped working for some reason today) – chobo2 Nov 21 '13 at 18:13
  • @techloverr - ok this is just weird, It seems that zune is messing things up. I plugged in my device and zune loads up. If I close zune down and then go to my device and type in my comps ip address it works with full debugging support. I reload zune(by replugging device in it does not work). However when I load up my application it connects to my web api BUT without debugging so I don't have a full solution yet. – chobo2 Nov 21 '13 at 18:28
  • @chobo2 are you using winphone 8 or 7? – techloverr Nov 22 '13 at 04:23
  • I am using 7 (Lumia 800) – chobo2 Nov 22 '13 at 07:43

5 Answers5

1

Your 404 says invalid hostname. Maybe you'll have to follow the steps described here In short: add the name of your machine to the config of your web service. It is about Windows Phone 8 but may be it'll work for Windows Phone 7 as well.

venerik
  • 5,766
  • 2
  • 33
  • 43
  • I saw that post but I was unclear on what happens when I launch VS (will it show localhost?) I was unclear on what I would use as the link in my WP7 browser. With ip I have a bit of success once I close zune down but while it is open it fails. – chobo2 Nov 22 '13 at 17:49
1

You didn't send the request details but from the device it is meaningless to call localhost. You need to connect to the remote machine which is your PC name or IP.

If you still can't reach the site with IP I would try setup Fiddler on WP7 to have a better understanding what's happening. Fiddler with windows phone.

Andras Csehi
  • 4,305
  • 1
  • 28
  • 36
0

How do you run your webapi? With IIS Express?

Configure IIS Express for external access to VS2010 project

Or with IIS? You will also encounter CORS issues. Is it webapi 1 or 2?

You probably want this nuget package: https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Cors/

read more about CORS(cross-origin resource sharing) support in Webapi2 here: http://msdn.microsoft.com/en-us/magazine/dn532203.aspx

if these pointers did not help you, I'd like to see some code calling the webapi and the webapi constructor and dataannotations

Community
  • 1
  • 1
JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
  • Yes I am using IIS express and pretty much did that(works fine when I used VS 2010 and WP&). I am using webapi 1 and have no clue what CORS is. I guess I could get some code up. – chobo2 Feb 13 '14 at 17:16
  • Ok, I added some sample code on how I would call my to my webapi service. – chobo2 Feb 14 '14 at 06:47
0

Please check your configuration in C:\Windows\System32\drivers\etc\hosts and also you could try to manipulate LAN Settings in IE Internet Properties.

Check as special Automatic configuration section and Automatically detect settings.

maciejgos
  • 103
  • 1
  • 10
0

You need to do this

<bindings>
    <binding protocol="http" bindingInformation="*:55210:localhost" />
    <binding protocol="http" bindingInformation="169.254.80.80:55210:" />
</bindings>

instead of this

<bindings>
    <binding protocol="http" bindingInformation=":55210:localhost" />
    <binding protocol="http" bindingInformation=":55210:169.254.80.80" />
</bindings>

Also, start visual with admin option... iis express works that way, it's a security issue.

Peter
  • 8,776
  • 6
  • 62
  • 95