title: HTTP POST Requests in Silverlight
body: <p>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.</p>
  
  <p>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</p>
  
  <pre><code>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);
  
  }
  </code></pre>