从Silverlight调用外部网站 [英] calling external websites from silverlight

查看:74
本文介绍了从Silverlight调用外部网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小型的Silverlight应用程序,只是为了尝试Silverlight.我的想法是制作一个小型应用程序,以检查网站是否在线.用户输入网址即可正常运行,我的应用程序每5分钟检查一次正常运行时间.

I am writing a small silverlight app just to try silverlight. My idea is to make a small app that checks if websites are online. It works by the user inputting a URL and my app checks it uptime every 5 minutes.

但是,每当我执行webrequest时,都会收到以下安全异常.阅读 http://msdn.microsoft. com/en-us/library/system.net.httpwebrequest(VS.95).aspx 似乎表明silverlight不允许跨域连接.因此,没有办法使我的想法在Silverlight中发挥作用吗?

But when ever I do a webrequest I get the security exception below. Reading http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(VS.95).aspx it seems to indicate that silverlight doesn't allow crossdomain connection. So is there no way to make my idea work in silverlight?

示例代码:

     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
        }

    }

例外: {System.Security.SecurityException ---> System.Security.SecurityException:安全错误. 在System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) 在System.Net.Browser.BrowserHttpWebRequest处.<> c__DisplayClass5.b__4(对象sendState) 在System.Net.Browser.AsyncHelper中.<> c__DisplayClass2.b__0(对象sendState) ---内部异常堆栈跟踪的结尾--- 在System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,对象状态) 在System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在monitor.Home.doCheck(IAsyncResult a)}

Exception: {System.Security.SecurityException ---> System.Security.SecurityException: Security error. at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.b__4(Object sendState) at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.b__0(Object sendState) --- End of inner exception stack trace --- at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at monitor.Home.doCheck(IAsyncResult a)}

推荐答案

您无法在Silverlight 3中覆盖服务器的跨域策略.在

You can't override the cross domain policy of the server in Silverlight 3. In Silverlight 4 you can with a trusted "out of browser" application.

您最好的选择是创建一个与托管Silverlight应用程序的域在同一域上运行的服务,并对其进行检查.

Your best bet is to create a service that runs on the same domain as the one hosting the Silverlight application and have it do the checks.

这篇关于从Silverlight调用外部网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆