Silverlight 的 WebClient 未连接到我的服务器

发布于 2024-07-15 06:02:21 字数 2377 浏览 2 评论 0原文

我这里有问题。

我有一个托管 silverlight 2 应用程序的 ASP.net 网站。 我希望该网站能够与 silverlight 应用程序进行来回通信,并且我通过 http 请求来完成此操作。 顺便说一句,如果有人知道更好的方法,请告诉我。

我的服务器设置了以下 http 侦听器。 我从某个教程网站复制了这个,因为目前主要是实验:

      HttpListener listener = new HttpListener (  );
      listener.Prefixes.Add("http://localhost:4531/MyApp/");  
      listener.Start(  );                                         

      // Wait for a client request:
      HttpListenerContext context = listener.GetContext(  );

      // Respond to the request:
      string msg = "You asked for: " + context.Request.RawUrl;
      context.Response.ContentLength64 = Encoding.UTF8.GetByteCount (msg);
      context.Response.StatusCode = (int) HttpStatusCode.OK;

      using (Stream s = context.Response.OutputStream)
      using (StreamWriter writer = new StreamWriter (s))
        writer.Write (msg);

      listener.Stop(  );

我使用以下代码发送请求:

 private void MyButton_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            b.Content = "Hello World";

            Uri serviceUri = new Uri("http://localhost:4531/MyApp/");
            WebClient downloader = new WebClient();
            downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(TestDownloadStoriesCompleted);
            downloader.DownloadStringAsync(serviceUri);

        }
        void TestDownloadStoriesCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                TextBox1.Text = e.Result;
            }
        }

我的问题是我可以使用几乎相同的代码从控制台应用程序连接到网络服务器(我通过在代码中设置断点来测试它),但是当我在 silverlight 中单击按钮时没有任何反应。 (我添加了“Hello World”来测试我确实将委托连接到按钮。)

我已经读到 silverlight 需要通过 webclient 进行连接的策略,但如果我使用的是,则不应出现这种情况服务器和 silverlight 应用程序具有相同的服务器和相同的域!

感谢你的回复!

编辑:我收到此异常:

System.Security.SecurityException ---> System.Security.SecurityException:安全错误。

另外,根据我的阅读显然是site-of-origin,xap的部署URI和请求URI也必须是相同的端口。

但是,当我设置要在特定端口上托管的服务器的属性,并将侦听器设置为侦听同一端口时,它会失败并显示消息“该进程无法访问该文件,因为该文件正在被另一个进程使用” 。 我认为这是因为 http 侦听器无法侦听用于托管它的同一端口:| 但是我怎样才能让 Silverlight 执行原始主机 Web 客户端请求呢?

I've got a problem here.

I've got an ASP.net website hosting a silverlight 2 application.
I'd like the site to communicate to and fro from the silverlight app, and I'm doing this via http requests. Incidentally, if anyone knows a better way, please do tell me.

My server's got the following http listener set up. I copied this from a tutorial site somewhere, since it's mainly experimentation at the moment :

      HttpListener listener = new HttpListener (  );
      listener.Prefixes.Add("http://localhost:4531/MyApp/");  
      listener.Start(  );                                         

      // Wait for a client request:
      HttpListenerContext context = listener.GetContext(  );

      // Respond to the request:
      string msg = "You asked for: " + context.Request.RawUrl;
      context.Response.ContentLength64 = Encoding.UTF8.GetByteCount (msg);
      context.Response.StatusCode = (int) HttpStatusCode.OK;

      using (Stream s = context.Response.OutputStream)
      using (StreamWriter writer = new StreamWriter (s))
        writer.Write (msg);

      listener.Stop(  );

I'm using the following code to send a request :

 private void MyButton_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            b.Content = "Hello World";

            Uri serviceUri = new Uri("http://localhost:4531/MyApp/");
            WebClient downloader = new WebClient();
            downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(TestDownloadStoriesCompleted);
            downloader.DownloadStringAsync(serviceUri);

        }
        void TestDownloadStoriesCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                TextBox1.Text = e.Result;
            }
        }

My problem is that I can connect to the webserver from a console application using pretty much the same code (I tested it by setting a breakpoint in the code), however nothing happens when I click the button in silverlight. (I've added the "Hello World" to test that I am indeed connecting the delegate to the button.)

I've read that silverlight needs policies to connect via webclient, but it shouldn't be the case if I'm using the same server and the same domain for both the server and the silverlight application!

Thanks for all your replies!

EDIT : I am recieving this exception :

System.Security.SecurityException ---> System.Security.SecurityException: Security error.

Also, based on what I'm reading apparently to be site-of-origin, the deployment URI of the xap and the request URI must also be of the same port.

However, when I set the properties for the server to be hosted on a specific port, and I set the listener to listen to that same port, it fails with the message that The process cannot access the file because it is being used by another process. I assume it is because the http listener can't listen to the same port being used to host it :|
But then how can I make Silverlight perform host of origin webclient requests?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉默的熊 2024-07-22 06:02:21

由于这只是一个测试,因此添加“else TextBox1.Text=e.Error.ToString();” 在 TestDownloadStoriesCompleted 处理程序中查看出现的错误。

编辑:

您不能在同一端口上托管 asp.net 应用程序和侦听器 - 您可以通过使用不同的端口并从 httplistener 提供 clientaccesspolicy.xml 来解决此问题。

不过,我认为您了解一下 WCF Web 服务(将 svc 添加到您的 asp.net 应用程序)会更有意义。 这是 示例

Since this is only a test add an "else TextBox1.Text=e.Error.ToString();" in your TestDownloadStoriesCompleted handler to see what error you get.

EDIT:

You can't host both the asp.net app and your listener on the same port - you could fix this by using a different port and serving a clientaccesspolicy.xml from your httplistener.

However I think it would make more sense for you to take a look at WCF web services (you add the svc to your asp.net app). Here's a sample.

薆情海 2024-07-22 06:02:21

您可以使用 http://www.fiddler2.com/fiddler2/ 等工具
实际查看请求期间发生了什么......
这可以为进一步调试提供一些帮助......

you can use tools like http://www.fiddler2.com/fiddler2/
to actually see what is going on during the request....
This can give some help for further debugging...

眼眸里的那抹悲凉 2024-07-22 06:02:21

我现在使用 HTTP 处理程序进行通信。 尽管我仍然想尝试一些 WCF,但它们似乎足以满足我的目的。

I am now using HTTP handlers for communication. It seems that they will work fine enough for my purpose, although I still want to try out some WCF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文