我正在编写一个小引擎来从一些.php文件下载文本,我在Visual c#中完成了这个引擎,我没有遇到问题。
我这样做:
[ ... ]
WebClient client = null;
try {
client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler( CompleteDownload );
client.AllowReadStreamBuffering = true;
client.DownloadStringAsync( new Uri( "http://blabla/bla.php" ) );
}
catch (Exception e) {
lbl1.Text = e.Message;
}
[ ... ]
这是“抓住”下载的数据:
public void CompleteDownloadPcops( object sender, DownloadStringCompletedEventArgs ea ) {
if ( ea.Error == null ) {
try{
lbl1.Text = ea.Result;
}
catch(Exception e) {
lbl2.Text = e.Message;
}
}
}
执行此代码我在lbl1
Exception has been thrown by the target of an invocation
上获得了lbl1.Text = ea.Result;
CompleteDownload
的{{1}}结果。为什么?并且,在知道原因之后,我该如何解决它?
更多信息:我在Ubuntu 11.04平台上使用monodevelop 2.4中的月光。
更新
我建议你将我的系统更新到MonoDevelop 2.6。现在,做同样的事情,我在ea.Error
上收到错误。消息是(西班牙文):
System.Security.SecurityException ---> System.Security.SecurityException: Error de seguridad.
en System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
en System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
en System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__1(Object sendState)
--- Fin del seguimiento de la pila de excepciones internas ---
en System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
en System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
en System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
en System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)
。
我现在使用的完整代码是:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
WebClient client = null;
try
{
client = new WebClient();
client.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.AllowReadStreamBuffering = true;
client.DownloadStringAsync(new Uri("http://carles.lambdafunction.com/a/try.php"));
}
catch (Exception ex)
{
lbl1.Text = ex.Message;
btn1.Content = "A";
}
}
void client_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
try
{
lbl2.Text = e.Result;
}
catch (Exception ex)
{
lbl1.Text = ex.Message;
lbl2.Text = ex.InnerException.ToString();
btn1.Content = "C";
}
}
else
{
lbl1.Text = e.Error.ToString();
btn1.Content = "B";
txt1.Text = e.Error.ToString();
}
}
}
您可以看到 Web调用的输出(到虚拟页面/p/try.php),它非常简单。真的,现在,我迷失了,因为我正在学习本教程:http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-3-using-networking-to-retrieve-data-and-populate-a-datagrid.aspx。
答案 0 :(得分:0)
您的.xap在哪里?
如果您尝试从其他网站(地址/端口)读取数据,请务必阅读 “Network Security Access Restrictions in Silverlight”并提供允许应用程序访问您的Web服务器的策略文件。
E.g。以下URL都返回404(这意味着没有允许Silverlight或Moonlight应用程序访问服务器的策略)