locked
Access web service on localhost from Android device RRS feed

  • Question

  • User767580559 posted

    I'm attempting to make ajax calls from a android device to a web service that's hosted locally (i.e. localhost). However, receiving the following error:
    Refused to connect to 'http://localhost:52437/MyServiceContract.svc/Read_Customer/11sj82' because it violates the following Content Security Policy directive: "connect-src
    'self' http://localhost:52437/MyServiceContract.svc".

    I've included the web service in a CSP directive via meta tag in the app but to no avail. Here's sample html tag:

    <meta http-equiv="Content-Security-Policy" content="connect-src 'self' http://localhost:52437/MyContract.svc; script-src 'self' https://maps.googleapis.com; default-src 'self' https://ssl.gstatic.com data: gap: 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">

    I've tried to use ADB reverse socket whereby the port no. for localhost is added. Here's a sample of the adb command:

    adb.exe reverse tcp:4000 tcp:4000

    After doing so, I'm able to access and retrieve data from the web service via web browser on the  device.

    In lieu of including the directives in the meta tag, I also tried using the config.xml but to no avail.

    Lastly, I've been able to successfully access the web service from the simulator.


    Any help is appreciated.

    Tuesday, May 8, 2018 12:21 AM

Answers

  • User767580559 posted

    Hi Edward,

    Yep, you were correct. It was the CSP. I had to add to directives to both config.xml and meta tag in order for it run on the device. Thanks to all for their help. It's appreciated.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 16, 2018 5:19 PM

All replies

  • User1168443798 posted

    Hi wrappingduke,

    For accessing web service from Android device, you need to publish your web service to public address and access the web service by the public IP address.

    Best Regards,

    Edward

    Tuesday, May 8, 2018 2:03 AM
  • User767580559 posted

    Hi Edward,

    Thanks for the reply. It's appreciated. Oh, that's not good. I was hoping to debug on the device using localhost. I wonder why I am able to access the web service from the device's browser and not the app?

    Tuesday, May 8, 2018 7:25 PM
  • User1168443798 posted

    Do you develop with Android or Xamarin.Forms?

    I suggest you try to publish your service to local IIS, and change the localhost to ip address when access web service from Android.

    In addition, how did you access web service from Anfroid.

    Wednesday, May 9, 2018 7:26 AM
  • User1120430333 posted

    Your android device should be accessing the same network as the machine hosting the Web service. That means that most likely you'll need a wireless router assuming that the Android device has wireless capabilities so that both the Windows machine and the Android device are part of the same Local Area Network (LAN) where you can give the LAN IP of the Windows machine hosting the service as part of the URL the Android device is using.

    Wednesday, May 9, 2018 4:34 PM
  • User767580559 posted

    Hi Edward,

    Thanks for the reply. I appreciate it. The app is developed using jquery mobile. It's appears that the issue maybe with the URI for the directive in the meta tag.  The name of the service was removed from the uri and the following error was returned:

    Apache Cordova: Failed to load resource: the server responded with a status of 404 (Not Found)

    I also moved the service to IIS and received the same error.

    Before making the aforementioned changes, I was able to access the service from the device using Chrome. However, in order to access the service using localhost, reverse socket was used via adb.exe command.

    Wednesday, May 9, 2018 5:44 PM
  • User1168443798 posted

    Hi wrappingduke,

    Since you could access the web service from device using Chrome, I think your issue is more related with Jquery Mobile developing.

    I would suggest you contact Jquery Moible support for help. They are more familiar with their  product.

    Best Regards,

    Edward

    Thursday, May 10, 2018 7:10 AM
  • User767580559 posted

    Hi Edward,

    I am not sure if jquery mobile is the issue since I was able to access the service in the simulators.

    Regards,

    Friday, May 11, 2018 5:12 PM
  • User1168443798 posted

    Hi wrappingduke,

    Which technology did you use to develop Android app?

    In general, we need to make sure the app could connect the host which hosts the service. 

    If you could access the service address from web browser in Androd, but fail to access the service in your Androd app, I think your issue is more related with network mapping in Androd app developing instead of WCF Service.

    Best Regards,

    Edward

    Monday, May 14, 2018 4:28 AM
  • User-369506445 posted

    hi

    if you hosted your service on local and you called it from android device, you must change

    http://localhost:52437/MyServiceContract.svc".

    to

    http://10.0.2.2:523/MyServiceContract.svc".

    The reason why you must use 10.0.2.2  is because it's the special alias to your host loopback interface

    Monday, May 14, 2018 4:47 AM
  • User767580559 posted

    Hi Vahid,

    Thanks for the reply. It's appreciated. The url was changed to access the service on iis and the same error was returned. I believe a different error would've been received if the url was not changed.

    Monday, May 14, 2018 5:23 PM
  • User-369506445 posted

    no,it not matter to change iis , i said if you are use a local server for example 192.168.1.2 or 192.168.10.50 or ...

    if it's local you can replace it with 10.0.2.2

    also for test you can publish your web service on a host and call with domain addres

    Monday, May 14, 2018 5:32 PM
  • User767580559 posted

    Thanks for the reply. Sorry, I wasn't more specific when I mentioned the service is on IIS. The service is being hosted with a call to the domain address in the url and the same error was returned. Here's a sample of the code:

    <meta http-equiv="Content-Security-Policy" content="connect-src 'self' http://www.mywebsite.com; script-src 'self' https://maps.googleapis.com; default-src 'self' https://ssl.gstatic.com data: gap: 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">

    Service call:

    var sUrl = "http://www.mywebsite.com/pathtohost/services/MyServiceContract.svc/Read_Customer/" + $("#txtDeviceId").val();
    
    var jqxhr = $.getJSON(sUrl, function () {
                // do nothing
            alert('this is a test');
        }).done(function (data) { ...}
        

    Monday, May 14, 2018 6:31 PM
  • User1168443798 posted

    Based on the error message, it is related with Content Security Policy, to check whether it is related with WCF Service, if you requst a new URL which is hosted in your domain site, will you receive the same error?

    Wednesday, May 16, 2018 3:12 AM
  • User767580559 posted

    Hi Edward,

    Yep, you were correct. It was the CSP. I had to add to directives to both config.xml and meta tag in order for it run on the device. Thanks to all for their help. It's appreciated.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 16, 2018 5:19 PM