1

I am getting 'connection refused' or 'bad request' when trying to connect from the android emulator to my web service on localhost. I have added the port number to the firewall to allow connections, and I have tried the advice given here to modify applicationhost.config

Both the web service and the emulator are running on the same (Windows 10) PC. In the server's web.config I have the following under 'system.webserver':

 <httpProtocol>
 <customHeaders>
  <clear />
  <add name="Access-Control-Allow-Origin" value="*" />
   <add name="Access-Control-Allow-Headers" value="Content-Type" />
   <add name="Access-Control-Allow-Methods" value="GET, PUT, POST" />
 </customHeaders>
 </httpProtocol>

AFAIK the emulator can find the web server (I'm not getting a 'not found' error), but the server refuses a connection.

   using (WebClient client = new WebClient())
   {
     string json = ......;
     client.Headers[HttpRequestHeader.ContentType] = "application/json";
    //      I have also tried  "application/x-www-form-urlencoded";
    string result = client.UploadString(UrlBase.urlBase + "SaveLocation", json);

If 'UrlBase' is http://192.168.1.73:60080/Service1.svc/, I get 'Bad request', which I know can result from badly formatted json.

If 'UrlBase' is a remote server, the connection works (so I know the json format is correct). Same updated dll running on both remote and local server.

If 'UrlBase' is http://localhost:60080/Service1.svc/, I get exception 'connection refused'

quilkin
  • 874
  • 11
  • 31
  • Please show the used code and used address. – greenapps Nov 03 '16 at 11:55
  • And what is unclear: Is the emulator running on the same pc as your webservice? If you have three pc's/laptops then all of them are their own localhost. So if you talk about localhost then how would we know what you mean? – greenapps Nov 03 '16 at 11:56
  • the emulator is it's own VM, and has it's own IP, so localhost refers back to the emulator. If you want to connect to the PC running the emulator, use it's IP or FQDN. – Jason Nov 03 '16 at 13:15
  • Thanks , more info now added into original post. I know the emulator's IP, but how do I tell the server to accept connections from it? – quilkin Nov 03 '16 at 14:58

1 Answers1

2

I am not using any firewall rules because my emulator is on the same machine as the service host. As Jason said you should use your host IP address. To avoid many problems use local IIS host not IIS express. Your "Bad request" is not invalid Json but Invalid host name because IIS express will not allow ANY connections but from the local machine. You can try to trick it as in the link you referenced above but why bother?

This is the result of IIS Express hosted app.

enter image description here

This is the result of local IIS hosted app

enter image description here

After getting the project here is the result:

enter image description here

Yuri S
  • 5,355
  • 1
  • 15
  • 23
  • OK, I have now installed IIS, and the properties/web page of the web project now allows me to choose 'Local IIS' as the server (it wasn't available before - can't think why VS2015 doesn't enable IIS by default if it's so important). I have created a website in IIS, and if I browse it within IIS manager I can access the site OK. But now when I try to run the service in VS2015 I just get error 404. There's a lot to learn in IIS; e.g. I'm unsure what to enter for 'project Url' on the web properties page (currently I have http://localhost/quilkin where quilkin is the name of the new IIS site. – quilkin Nov 08 '16 at 22:42
  • First lets fix VS for you. Here is what you need to do. Open VS2015 Command prompt and run "aspnet_regiis.exe -i" Now you should be able to select local IIS from VS. Try that and let me know what do you get. I think you are not specifying correct address of your service. It should include your App name. You should get that when you run it from VS. – Yuri S Nov 08 '16 at 23:19
  • I created it as web api, your looks like svc service. Is it WCF? If you can share your web service sample project I can run it and tell you exactly what to do in Xamarin client. – Yuri S Nov 08 '16 at 23:27
  • You can try "http://localhost/quilkin/Service1.svc"c on PC and if you see it then "http://descktopIpAddress/quilkin/Service1.svc" on emulator – Yuri S Nov 08 '16 at 23:32
  • Thanks for suggestions, Yuri. I already installed IIS and it was selected; I tried running aspnet_regiis but was told to use Windows Features instead, which is what I'd done yesterday. Tried adding 'service1.svc' to the Url but no success. The web project is at https://github.com/quilkin/Location , folder 'webmap'. Please ignore other projects in there, they are out-of-date, for some reason the xamarin client project has lost its connection to GitHub (so a need for a new stackoverflow question!). . – quilkin Nov 09 '16 at 16:43
  • Should I use MapServer as your project service? – Yuri S Nov 09 '16 at 16:49
  • Just managed to upload the xamarin project : https://github.com/quilkin/LocationService/tree/master/DemoService – quilkin Nov 09 '16 at 16:53
  • No, the project I'm trying to debug is in 'WebMap'; the others are out-of-date attempts which I will delete when everything works! – quilkin Nov 09 '16 at 16:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127735/discussion-between-yuri-s-and-quilkin). – Yuri S Nov 09 '16 at 17:13
  • @quilkin moved discussion to chat – Yuri S Nov 09 '16 at 17:16
  • Added picture to answer – Yuri S Nov 09 '16 at 17:27
  • many thanks for your help yesterday, I have narrowed down the issue and posted a new thread http://stackoverflow.com/questions/40524511/cannot-run-asp-net-service-on-iis-windows-10-error-404-not-found – quilkin Nov 10 '16 at 09:55