2011-06-17 166 views
0

我正在研究一個Silverlight應用程序,該應用程序具有一個表單,該表單應該使用POST方法將表單數據發送到PHP頁面。Silverlight中的HTTP POST請求

我正在使用下面的代碼,它給了我一個安全異常。我認爲這是一個跨域錯誤。我也查看了localhost上的視圖,但力度不夠。 SOS

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost/wb/cam.php", UriKind.Absolute)); 
       request.Method = "POST"; 
       // don't miss out this 
       request.ContentType = "application/x-www-form-urlencoded"; 
       request.BeginGetRequestStream(new AsyncCallback(RequestReady), request); 

    void RequestReady(IAsyncResult asyncResult) 
{ 

    HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest; 
    Stream stream = request.EndGetRequestStream(asyncResult); 

    // Hack for solving multi-threading problem 
    // I think this is a bug 
    this.Dispatcher.BeginInvoke(delegate() 
    { 
     // Send the post variables 
     StreamWriter writer = new StreamWriter(stream); 
     writer.WriteLine("imgdata="+textBox1.Text); 
     writer.Flush(); 
     writer.Close(); 

     request.BeginGetResponse(new AsyncCallback(ResponseReady), request); 
    }); 
} 

// Get the Result 
void ResponseReady(IAsyncResult asyncResult) 
{ 
    try 
    { 
     HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest; 
     HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult); 

     this.Dispatcher.BeginInvoke(delegate() 
     { 
      Stream responseStream = response.GetResponseStream(); 
      StreamReader reader = new StreamReader(responseStream); 
      // get the result text 
      string result = reader.ReadToEnd(); 
     }); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

private void OnCaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e) 
{ 
      btnSnapshot.IsEnabled = true; 
      webCamVRect.Background = new ImageBrush {ImageSource = e.Result}; 
     } 

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost/wb/cam.php", UriKind.Absolute)); 
    request.Method = "POST"; 
    // don't miss out this 
    request.ContentType = "application/x-www-form-urlencoded"; 
    request.BeginGetRequestStream(new AsyncCallback(RequestReady), request); 

} 
+2

請發佈該異常的詳細信息,哪一行是拋出異常,什麼是確切的異常消息。 – 2011-06-17 10:19:34

回答

0

您將需要一個clientaccesspolicy.xml添加到您的服務器正在接收請求的根。還要注意虛擬路徑,因爲有時候clientaccesspolicy會被放到虛擬路徑中,而不是放在它應該存在的根目錄中。祝你好運!

提姆