Calling external websites from silverlight

I am writing a small silverlight application to try silverlight. My idea is to make a small application that checks if websites are on the Internet. It works from the user entering the URL, and my application checks its operation every 5 minutes.

But when I ever make a web request, I get a security exception below. A reading of http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(VS.95).aspx seems to indicate that silverlight does not allow cross-domain connections. So there is no way to get my idea to work in Silverlight?

Code example:

WebRequest testHTTP = null; try { testHTTP = WebRequest.Create(serverToCheck); } catch (UriFormatException ufe) { try { testHTTP = WebRequest.Create("http://" + serverToCheck); } catch (UriFormatException ufe1) { MessageBox.Show("Invalid server address"); } } if (testHTTP != null) { testHTTP.BeginGetResponse(new AsyncCallback(doCheck), testHTTP); } void doCheck(IAsyncResult a) { try { HttpWebRequest req = (HttpWebRequest)a.AsyncState; HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(a); Dispatcher.BeginInvoke(() => HTTPStatus.Content = "OK"); } catch (Exception ex) { //handle exception } } 

Exception: {System.Security.SecurityException ---> System.Security.SecurityException: security error. in System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse (IAsyncResult asyncResult) in System.Net.Browser.BrowserHttpWebRequest. <> c__DisplayClass5.b__4 (Object sendState) in System.Net.Browser.AsyncHelper. <> c__DisplayClass2.b__0 (Object sendState) --- The end of the internal check of the exception stack --- in System.Net.Browser.AsyncHelper.BeginOnUI (SendOrPostCallback beginMethod, object state) in System.Net.Browser.BrowserHttpWebRequest.EndGetResponseessecsynsrssultsultesultesynessultsynsrcultsynsesultsultsynsrssultsultsultsynssyncultsultsynsultsynsrssultsultsultsultsultsultsultesultesultesultesultesultsynsr ) on the monitor .Home.doCheck (IAsyncResult a)}

+1
source share
4 answers

You cannot override the cross-domain policy on a Silverlight 3 server. In Silverlight 4, you can with a trusted "from the browser".

It’s best to create a service that runs in the same domain as the one hosting the Silverlight application and perform validations.

+3
source

You can make a server page in ASP.Net that connects to any website and tells you what it is. Since it will be hosted in the same domain as your Silverlight application, it will work fine. (ASP.Net does not have such restrictions)

Please note that then it will check if the server can connect to the website and not to the client.

0
source

The sites you are trying to access require a cross-domain policy file to allow access. Harsh sites are likely to have no such sites, so what you are trying to do is blocked.

0
source

I assume that the target URI needs a clientaccesspolicy.xml or crossdomain.xml file in the root. The solution might be to create a WCF service located on your server that processes the request.

0
source

Source: https://habr.com/ru/post/1383481/


All Articles