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:
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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
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?
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.
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
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.
If you find the post has answered your issue, then please mark post as 'answered'.
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.
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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
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.
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:
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) { ...}
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?
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
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.
Member
4 Points
104 Posts
Access web service on localhost from Android device
May 08, 2018 12:21 AM|wrappingduke|LINK
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:
I've tried to use ADB reverse socket whereby the port no. for localhost is added. Here's a sample of the adb command:
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.
Contributor
3310 Points
1607 Posts
Re: Access web service on localhost from Android device
May 08, 2018 02:03 AM|Edward Z|LINK
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
4 Points
104 Posts
Re: Access web service on localhost from Android device
May 08, 2018 07:25 PM|wrappingduke|LINK
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?
Contributor
3310 Points
1607 Posts
Re: Access web service on localhost from Android device
May 09, 2018 07:26 AM|Edward Z|LINK
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.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Contributor
4973 Points
4263 Posts
Re: Access web service on localhost from Android device
May 09, 2018 04:34 PM|DA924|LINK
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.
Member
4 Points
104 Posts
Re: Access web service on localhost from Android device
May 09, 2018 05:44 PM|wrappingduke|LINK
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.
Contributor
3310 Points
1607 Posts
Re: Access web service on localhost from Android device
May 10, 2018 07:10 AM|Edward Z|LINK
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
4 Points
104 Posts
Re: Access web service on localhost from Android device
May 11, 2018 05:12 PM|wrappingduke|LINK
Hi Edward,
I am not sure if jquery mobile is the issue since I was able to access the service in the simulators.
Regards,
Contributor
3310 Points
1607 Posts
Re: Access web service on localhost from Android device
May 14, 2018 04:28 AM|Edward Z|LINK
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Star
8119 Points
2778 Posts
Re: Access web service on localhost from Android device
May 14, 2018 04:47 AM|vahid bakkhi|LINK
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
Please MARK AS ANSWER if suggestion helps.
Member
4 Points
104 Posts
Re: Access web service on localhost from Android device
May 14, 2018 05:23 PM|wrappingduke|LINK
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.
Star
8119 Points
2778 Posts
Re: Access web service on localhost from Android device
May 14, 2018 05:32 PM|vahid bakkhi|LINK
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
Please MARK AS ANSWER if suggestion helps.
Member
4 Points
104 Posts
Re: Access web service on localhost from Android device
May 14, 2018 06:31 PM|wrappingduke|LINK
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:
Service call:
Contributor
3310 Points
1607 Posts
Re: Access web service on localhost from Android device
May 16, 2018 03:12 AM|Edward Z|LINK
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?
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
4 Points
104 Posts
Re: Access web service on localhost from Android device
May 16, 2018 05:19 PM|wrappingduke|LINK
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.