如何使 HTTP PUT 和 DELETE 在 Silverlight 4 中工作

发布于 2024-12-05 01:13:04 字数 1066 浏览 5 评论 0原文

我宁愿避免陷入关于 HTTP 动词 PUT 和 DELETE 是否合适或过时的争论,而是关注当“被迫”使用这些动词时使 Silverlight 实际工作的问题。

我正在尝试创建一个 Silverlight 4 客户端应用程序,该应用程序调用现有的 REST Web 服务,该服务具有 PUT 和 DELETE 动词的操作。这项服务不会改变。

我已将以下语句添加到 App.xaml.cs 的构造函数中:

WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

该服务有一个 clientaccesspolicy.xml 文件,其中包含:

<?xml version="1.0" encoding="utf-8"?>
<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>

我正在使用 WebClient 类来处理 GET 和 POST 请求。当我尝试对动词/方法使用 PUT 或 DELETE 执行相同操作时,我收到一个不明确的“安全错误”,这导致我添加上面的语句。

我看过各种讨论使用 HttpWebRequest 来解决此问题的帖子和博客文章,但没有找到真正展示如何从 Silverlight 客户端进行这些(异步)调用的帖子和博客文章。

如果上面的代码有问题,请告诉我。否则,如果您可以向我展示或向我指出一个示例来演示如何实现这些请求,我将非常感谢您的帮助。

I would prefer to avoid getting into a debate about whether HTTP verbs PUT and DELETE are appropriate or obsolete and focus on the question of actually making Silverlight work when "forced" to use these verbs.

I am trying to create a Silverlight 4 client application that calls an existing REST web service that has operations for the PUT and DELETE verbs. This service is not going to change.

I've added the following statement into constructor in my App.xaml.cs:

WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

The service has a clientaccesspolicy.xml file that contains:

<?xml version="1.0" encoding="utf-8"?>
<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>

I am using the WebClient class to handle GET and POST requests. When I attempt to do the same with PUT or DELETE for the verb/method, I get an ambiguous "security error" which led me to adding the statement above.

I've seen various posts and blog articles talking about using HttpWebRequest to get around this but have not found one that actually SHOWS HOW to make these (asynchronous) calls from a Silverlight client.

If there is something wrong with the code above, please let me know. Otherwise, if you can show me or point me to an example demonstrating how these requests can be implemented, I'd greatly appreciate the help.

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

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

发布评论

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

评论(3

淡水深流 2024-12-12 01:13:04

clientaccesspolicy.xml 文件中,您必须允许 PUTDELETE HTTP 谓词。

我通常允许所有 HTTP 动词,在给定您的原始配置的情况下,它们看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="*" http-methods="*">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

请注意 allow-from 元素上添加的 http-methods 属性。

In your clientaccesspolicy.xml file, you must allow the PUT and DELETE HTTP verbs.

I usually allow all HTTP verbs, which would look like this given your original configuration:

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="*" http-methods="*">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

Notice the added http-methods attribute on the allow-from element.

鸢与 2024-12-12 01:13:04

我已经解决了我的问题,但仍然不能 100% 确定它为何得到解决。

根据同事的建议,我启用了在浏览器外运行应用程序,并检查了设置以在浏览器外运行时需要提升信任。该应用程序运行良好。我禁用浏览器外运行,但应用程序仍然运行良好!

正如设置所说,在浏览器之外运行时需要更高的信任度。因此,如果这是问题所在,那么我不确定在浏览器中运行时检查它是否应该解决我的问题。但它确实...

I've solved my problem but am still not 100% sure why it is fixed.

At the suggestion of a co-worker, I enabled running the application out-of-browser and checked the setting to require elevated trust when running outside the browser. The app ran fine. I disable running out-of-browser and the app still runs fine!

As the setting says, it requires elevated trust WHEN RUNNING OUTSIDE THE BROWSER. So, if this was the problem, then I'm not sure having it checked should be fixing my problem when running IN the browser. But it does...

浪漫人生路 2024-12-12 01:13:04

添加
<代码>
HttpWebRequest.RegisterPrefix("http://",WebRequestCreator.ClientHttp);
HttpWebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);

https://mattduffield.wordpress.com/2011/12/11/silverlight-specified-method-is-not-supported-on-this-request/

Add

HttpWebRequest.RegisterPrefix("http://",WebRequestCreator.ClientHttp);
HttpWebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);

https://mattduffield.wordpress.com/2011/12/11/silverlight-specified-method-is-not-supported-on-this-request/

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