2010-10-03 13 views
1

我試圖發送POST請求到一個PHP腳本(在這種情況下「http:// mywebsite/index/test /」),一旦單擊按鈕。我使用的Web客戶端的方法UploadStringAsync爲,但似乎並沒有工作: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>[email protected]</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) 
+0

你確定'button1_Click'實際上是否被調用? – dtb 2010-10-03 18:46:15

+0

WebClient曾經調用過'client_UploadStringCompleted'嗎? – dtb 2010-10-03 18:47:03

+0

@dtb是的,它調用client_UploadStringCompleted方法。我已經把HtmlPage.Window.Alert(「aaa」);在那裏,我會彈出警報窗口。 – 2010-10-03 20:05:49

回答

2

是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,你就能夠迅速地確認它是否確實是一個跨域問題。

+0

謝謝你解決了這個問題。 – 2010-10-03 21:39:54

+0

@Richard Knop,沒問題 – 2010-10-03 22:42:56