Silverlight中的HTTP POST请求
本文关键字:请求 POST HTTP 中的 Silverlight | 更新日期: 2023-09-27 17:59:18
我正在开发一个Silverlight应用程序,该应用程序具有一个Form,该Form应该使用POST方法将表单数据发送到PHP页面。
我正在使用以下代码,这给了我一个安全异常。我认为这是一个跨域错误。我也查看了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);
}
您需要将clientaccesspolicy.xml添加到接收请求的服务器的根目录中。还要注意虚拟路径,因为有时clientaccesspolicy会放在虚拟路径中,而不是应该放在根路径中。祝你好运!
Tim