开发者

HTTP POST Requests in Silverlight

I am working on a Silverlight application that has a Form that is supposed to send the form data to a PHP page with the POST method.

I am using the following code, which is giving me a Security exception. I assumed it to be a cross domain error. I checked the view on localhost as well, but dint work. 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);

}


You will need to add a clientaccesspolicy.xml to the root of your server that is receiving the request. Also beware of virtual paths as sometimes the the clientaccesspolicy gets put in the virtual path instead of in the root where it should be. Good luck!

Tim

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜