title: SecurityException from using HttpWebRequest cross-domain in Silverlight 4
body: <p>I'm trying to write a function in my Silverlight  app that requests a particular page that doesn't exist on the same domain as where my Silverlight app is hosted.</p>
  
  <p>For example:</p>
  
  <ul>
  <li>Silverlight App: <a href="http://www.mysilverlightsite.com/" rel="nofollow">http://www.mysilverlightsite.com/</a></li>
  <li>Target Page: <a href="http://www.mysite.com/MyPage.aspx" rel="nofollow">http://www.mysite.com/MyPage.aspx</a></li>
  </ul>
  
  <p>However, this generates a 'SecurityException':</p>
  
  <blockquote>
    <p>{System.Security.SecurityException:
    Security error.    at
    System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult
    asyncResult)    at
    System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult
    asyncResult) ...}</p>
  </blockquote>
  
  <p>From what I understand, this is related to cross-domain requests being restricted, and found some posts that mentioned that this article (http://msdn.microsoft.com/en-us/library/cc197955(VS.95).aspx) might be related.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>public static void CheckPageContentsAsync(CheckPageContentsCallback callback, DependencyObject uiObject)
  {
      bool result = false;
      try
      {
          HttpWebRequest request = WebRequest.CreateHttp("http://www.mysite.com/MyPage.aspx");
          request.BeginGetResponse((asyncHandle) =&gt;
          {
              try
              {
                  uiObject.Dispatcher.BeginInvoke(new VoidDelegate(() =&gt;
                  {
  
                      HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncHandle);
                      using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                      {
                          string content = sr.ReadToEnd();
                          if (content.Contains("Value"))
                          {
                              result = true;
                          }
  
                          if (callback != null)
                          {
                              callback.Invoke(result);
                          }
                      }
                  }), null);
              }
              catch (Exception excep)
              {
                  throw new Exception("Failed to process response.", excep);
              }
          }, null);
      }
      catch(Exception excep2)
      {
          throw new Exception("Failed to generate request.", excep2);
      }
  }
  </code></pre>
  
  <p>Haven't been able to make much sense of the applicability of the "clientaccesspolicy.xml" or "crossdomain.xml" files as a solution.</p>
  
  <p>Can anyone explain clearly how I modify my app, or the web server I'm requesting from, to resolve this issue?</p>