Skip to main content

Microsoft Silverlight

Answered Question WebClientRSS Feed

(0)

skywalker0121
skywalke...

Member

Member

0 points

3 Posts

WebClient

Dera All,

Follow code are copied from MSDN
http://msdn.microsoft.com/zh-tw/library/cc645034(VS.95).aspx
I know the basic programming and XAML,
so I don't key in the detail of the implement of the following code.

I use Visual Studio 2008 Express Edition to implement this project.
But it can't run.

Does following code have any problem?
Or it is just my problem.

<TextBlock x:Name ="OutputTextBlock" Canvas.Top ="10" TextWrapping="Wrap"/> 

Imports System.IO
Imports System.Xml.Linq
Imports System.Net

Dim wc As WebClient = New WebClient()
AddHandler wc.OpenReadCompleted, AddressOf wc_OpenReadCompleted
wc.OpenReadAsync(New Uri(uriString))

 Private Sub wc_OpenReadCompleted(ByVal sender As Object, ByVal e As OpenReadCompletedEventArgs)
    If e.Error IsNot Nothing Then
        OutputTextBlock.Text = e.Error.Message
        Return
    End If
    Using s As Stream = e.Result
        Dim doc As XDocument = XDocument.Load(s)
        OutputTextBlock.Text = doc.ToString(SaveOptions.OmitDuplicateNamespaces)
    End Using
End Sub

jay nanavati
jay nana...

Contributor

Contributor

3388 points

624 Posts

Re: WebClient

can you tell what kind of error you are getting?

Jay K Nanavaty
www.technologyopinion.com
Mark as answer if it helps. It will also help others...

skywalker0121
skywalke...

Member

Member

0 points

3 Posts

Re: Re: WebClient

System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult result)

Above Error from the following code:
If e.Error IsNot Nothing Then
            OutputTextBlock.Text = e.Error.Message & e.Error.StackTrace
            Return
End If

e.Error.Message is space or null.

jay nanavati
jay nana...

Contributor

Contributor

3388 points

624 Posts

Re: Re: Re: WebClient

uriString variable should contain valid value like this one:

http://feeds.feedburner.com/CommunitySilverlightContent

Jay K Nanavaty
www.technologyopinion.com
Mark as answer if it helps. It will also help others...

skywalker0121
skywalke...

Member

Member

0 points

3 Posts

Re: Re: Re: WebClient

I put a xml file in my skydrive web harddisk. And I set the url of the xml file for uriString.

I can see the xml file content in design time in the visual studio 2008 express edition. But I can't see the xml file content in run time in IE. And I got above error message.

Mog Liang - MSFT
Mog Lian...

Star

Star

14812 points

1,417 Posts

Answered Question

Re: Re: Re: WebClient

Hi Skywalker,

From your description, I understand that you encountered the problem when using WebClient to download xml file from SkyDrive. If I misunderstood you, please correct me.

Due to security reason, there is some restriction for silverlight accessing cross-domain resource.

  •  Before silverlight application download resources from target server in which is not silverlight application hosted, it will try to read security policy file from target server and determine if this access should be allowed.

From the error message you provided, I inferred that the target server doesn't provide policy file or policy doesn't allow your access.

To solve the problem,  we have several choices

  • create a policy file and deploy on root path of target server. 
       a sample policy file with most access permission 
    <access-policy>
      <cross-domain-access>
        <policy>
          <allow-from http-request-headers="*">
            <domain uri="*"/>
          </allow-from>
          <grant-to>
            <resource path="/" include-subpaths="true"/>
          </grant-to>
        </policy>
      </cross-domain-access>
    </access-policy>
     For details about policy file specification, please check this article    
  • move resource files to server in which your silverlight application hosted.
  • download resource files and embed into silverlight project, then we could get resource file by using App.GetResourceStream() method.

For more information about Network security access restriction in silverlight, please check this article

http://msdn.microsoft.com/en-us/library/cc645032(VS.96).aspx

Thanks,

Mog Liang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities