我正在寫一個小的silverlight應用程序來試試silverlight。我的想法是製作一個小應用程序,檢查網站是否在線。它通過用戶輸入一個URL來工作,我的應用程序每5分鐘檢查一次它的正常運行時間。從silverlight呼叫外部網站
但是,當我做網絡請求時,我會得到下面的安全例外。閱讀http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(VS.95).aspx它似乎表明silverlight不允許跨域連接。那麼沒有辦法讓我的想法在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:安全錯誤。 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 internal 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)}