2010-05-03 24 views
5

Silverlight kullanarak bir sayfanın html içeriğini almaya çalışıyorum. Web yanıtları ve istek sınıfları gümüş ışıkta çalışmaz.Bir sayfanın html içeriğini Silverlight ile alın

Bazı çalışmalar yaptım ve bir şey buldum. Bu denedim budur:

Microsoft JScript çalışma zamanı hatası:

public partial class MainPage : UserControl 
{ 
    string result; 
    WebClient client; 

    public MainPage() 
    { 
    InitializeComponent(); 
    this.result = string.Empty; 
    this.client = new WebClient(); 
    this.client.DownloadStringCompleted += ClientDownloadStringCompleted; 
    } 

    private void btn1_Click(object sender, RoutedEventArgs e) 
    { 
    string url = "http://www.nu.nl/feeds/rss/algemeen.rss"; 

    this.client.DownloadStringAsync(new Uri(url, UriKind.Absolute)); 

    if (this.result != string.Empty && this.result != null) 
    { 
    this.txbSummery.Text = this.result; 
    } 
    } 

    private void ClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
    this.result = e.Result; 
    //handle the response. 
    } 
} 

O düğmesine bastıktan sonra bana bir çalışma zamanı hatası veriyor Silverlight Uygulamasında İşlenmemiş Hata istisna sonucu geçersiz kılarak, işlem sırasında meydana geldi. İstisna ayrıntıları için InnerException kontrol edin. en System.Net.WebClient.OnDownloadStringCompleted (DownloadStringCompletedEventArgs e) de JWTG.MainPage.ClientDownloadStringCompleted de System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() System.Net.DownloadStringCompletedEventArgs.get_Result() de (Obje gönderen, DownloadStringCompletedEventArgs e) de System.Net.WebClient.DownloadStringOperationCompleted (Object arg)

Çok sayıda deneme yaptım ama hepsi başarısız oldu.

Neyi eksik? Ya da bunu başka bir şekilde nasıl başarabileceğimi bilen var mı?

Şimdiden teşekkürler! Bu hat

this.client.DownloadStringAsync(new Uri(url, UriKind.Absolute)); 

bir arka plan iş parçacığı bir asynchroneous indir belirten edilir ise

+0

deneyin. Gönderdiğiniz kodun hatayla ilgisi yoktur. Snippet'ini denedim ve işe yarıyor. BTW: Bunu 'this.txbSummery.Text = this.result;' 'ClientDownloadStringCompleted' yöntemine taşımak zorundasınız. Şu anda, metni indirilmeden önce metin kutusuna koymaya çalışıyorsunuz. Bu açıkça işe yaramıyor. –

+0

Bir göz atın: http://forums.silverlight.net/forums/t/54721.aspx belki size yardımcı olur. Sorun, yanlış yapılandırılmış bir 'web.config 'dosyası olabilir. –

cevap

1

Bu, clientaccesspolicy.xml ile ilgilidir. Devamı burada: http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx

"If the connection request was from a WebClient or an HTTP class to a cross-domain site, the Silverlight runtime tries to download the security policy file using the HTTP protocol. The Silverlight runtime first tries to download a Silverlight policy file with a name of "clientaccesspolicy.xml" at the root of the requested target domain using the HTTP protocol.

If the "clientaccesspolicy.xml" is either not found (the web request returns a 404 status code), returned with an unexpected mime-type, is not valid XML, or has an invalid root node, then the Silverlight runtime will issue a request for a for the Flash policy file with a name of "crossdomain.xml" at the root of the requested target domain, using the HTTP protocol.

HTTP redirects for the policy file are not allowed. A redirect for a policy file will result in a SecurityException of access denied."

+0

Merhaba, Ben bunu anladım. Silverlight ile erişmeye çalıştığım web sitesi politika dosyasına sahip değil. Yine de teşekkürler! – Yustme

0

. Ve bir sonraki satırda bir şekilde tamamlanmasının beklendiğini mi düşünüyorsun?

Threading hakkında bilginiz yoksa önce DownloadString ile deneyin. O zaman kodunuz işe yarayacak.

+0

Merhaba, Bu 'sonraki satırı' indirmeyi geri çağırmaya koymaya çalıştım ancak bu çalışma zamanı hatasını düzeltti. Bu 'downloadstring'i nerede bulabilirim? Hangi sınıfa ait? – Yustme

+0

@Foxfire: Gerçekten bir web sayfasını almak için engelleme çağrısı yapmak istemiyorsunuz, değil mi? Yustme, sadece 'DownloadStringAsync' kullanmalı, ancak iade edilen verileri 'ClientDownloadStringCompleted' yöntemine geçirme mantığını koymalıdır. –

+0

@Yustme: Olayı daha sonra başka bir iş parçacığından çağrılacağı için olaya koyamazsınız. – Foxfire

1

Bunu, btn1_Click ve ClientDownloadStringCompleted yöntemleriniz yerine deneyin. Metin kutusunu güncellemek için feed indirildikten sonra GUI iş parçacığını çağırır. Ağdaki bir hata nedeniyle başarısız olursa, istisnayı (TargetInvocationException'da bir iç istisna olarak bulunur) ve iç istisnayı yeniden açar.

private void btn1_Click(object sender, RoutedEventArgs e) 
{ 
    string url = "http://www.nu.nl/feeds/rss/algemeen.rss"; 

    this.client.DownloadStringAsync(new Uri(url)); 
} 

private void ClientDownloadStringCompleted(object sender, 
         DownloadStringCompletedEventArgs e) 
{ 
    try 
    { 
     Dispatcher.BeginInvoke(() => this.txbSummery.Text = e.Result ?? ""); 
    } 
    catch (TargetInvocationException tiex) 
    { 
     throw tiex.InnerException; 
    } 
} 

hatası (ben bunun olacağını tahmin) tekrar occures Eğer

, burada bir döküm ve hata mesajı gönderin.

+0

Merhaba, Bu i elde ediyoruz o:. Silverlight Uygulama atmak yeni Hatası ("İşlenmemiş Hata istisna geçersiz sonuç yapım çalışması sırasında meydana gelen istisna detayları için InnerException edin. System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() \ n at System.Net.DownloadStringCompletedEventArgs.get_Result() \ n at JWTG.MainPage. <> c__DisplayClass2. b__0() "); hiçbir kural dışı durum kutusunda kutulu olan – Yustme

+0

Damn stacktrace yoktur. Yani belki içselSorum'a bir göz atabilir misin? Hata ayıklayıcısını kullanın ya da 'throw tiex.InnerException;' ile birlikte atın; tiex.InnerException.InnerException; ' –

+0

Hiçbir zaman bu kadar uzağa gitmez, Dispatcher.BeginInvoke durur (() => this.txbSummery.Text = e . Sonuç ?? ""); ama daha fazla bilgi için bu yazının başından bakın. – Yustme

1

Bunu Bir JScript hatası alıyorsanız

private void btn1_Click(object sender, RoutedEventArgs e) 
    { 
     string url = "http://www.nu.nl/feeds/rss/algemeen.rss"; 

     this.client.DownloadStringAsync(new Uri(url, UriKind.Absolute)); 

    } 

    private void ClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     Stream s = e.Result; 
     StreamReader strReader = new StreamReader(s); 
     string webContent = strReader.ReadToEnd(); 
     s.Close(); 
     this.txbSummery.Text =webContent; 

    } 
İlgili konular