【问题标题】:UploadStringAsync not workingUploadStringAsync 不工作
【发布时间】:2011-04-20 13:05:02
【问题描述】:

单击按钮后,我正在尝试向 PHP 脚本(在本例中为“http://mywebsite/index/test/”)发送 POST 请求。我正在使用 WebClient 的 UploadStringAsync 方法,但它似乎不起作用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyWebsite
{
    public partial class Home : Page
    {
        WebClient client = new WebClient();

        public Home()
        {
            InitializeComponent();
            client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // Create the request. 
            string postRequest = "<entry xmlns='http://www.w3.org/2005/Atom'>"
            + "<title type='text'>New Restaurant</title>"
            + "<content type='xhtml'>"
            + "  <div xmlns='http://www.w3.org/1999/xhtml'>"
            + "   <p>There is a new Thai restaurant in town!</p>"
            + "   <p>I ate there last night and it was <b>fabulous</b>.</p>"
            + "   <p>Make sure and check it out!</p>"
            + "  </div>"
            + " </content>"
            + " <author>"
            + "   <name>Pilar Ackerman</name>"
            + "  <email>packerman@contoso.com</email>"
            + " </author>"
            + "</entry>";

            // Sent the request to the specified URL.
            client.UploadStringAsync(new Uri("http://mywebsite/index/test/",
                UriKind.Absolute), postRequest); 
        }

        // Event handler for the UploadStringCompleted event.
        void client_UploadStringCompleted(object sender,
           UploadStringCompletedEventArgs e)
        {
            // Output the response. 
            if (e.Error != null)
                textBlock3.Text = e.Error.Message;
            else
                textBlock3.Text = e.Result;
        }

    }
}

我知道 PHP 脚本没有被调用,因为它在执行时会写入一个包含请求信息的文件。但是我在 Visual Studio 中没有收到任何错误或通知,我不确定可能是什么问题。

编辑:

我正在使用 Silverlight 4、IIS7、PHP 5.3.3。

编辑 2:

好的,我通过显示 e.Error.ToString() 设法得到了这个错误:

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

   at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)

   at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)

   at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)

   --- End of inner exception stack trace ---

   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)

   at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)

   at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)

   at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)

【问题讨论】:

  • 你确定button1_Click 真的被调用了吗?
  • WebClient 是否曾经调用过client_UploadStringCompleted
  • @dtb 是的,它调用了 client_UploadStringCompleted 方法。我放了 HtmlPage.Window.Alert("aaa");在那里,我得到了弹出警报窗口。
  • @dtb button1_Click 也被调用,我已经用 HtmlPage.Window.Alert("bbb"); 对其进行了测试。
  • 安装 Fiddler 并检查是否发送了请求。 WebClient 也是 IDisposable,你应该把它包装在 Using 语句中。

标签: c# silverlight silverlight-4.0 cross-domain


【解决方案1】:

Silverlight 是否与 Web 服务托管在同一个域中?如果不是这种情况,您需要创建一个 clientaccesspolicy.xml 文件。

See here

例子:

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

更新: 如果您安装Fiddler,您将能够快速确认它是否确实是一个跨域问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 2021-01-10
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    相关资源
    最近更新 更多