// Handle Request Stream writing public void RequestStreamCallback(IAsyncResult ar) { // Make sure the state is correct if (!(ar.AsyncState is RestCommandState)) { throw new Exception("Request Stream callback failed."); } // Get the web request RestCommandState reqState = (RestCommandState)ar.AsyncState; HttpWebRequest req = reqState.webRequest; byte[] data = reqState.requestData; // Write out request Stream requestStream = req.EndGetRequestStream(ar); requestStream.Write(data, 0, data.Length); requestStream.Close(); // Start the async result call req.BeginGetResponse(ResponseStreamCallback, reqState); System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => GamePage.gamePage.DebugTextbox.Text = "Sent Request"); }
// Handle HTTP response public void ResponseStreamCallback(IAsyncResult ar) { // Make sure the state is correct if (!(ar.AsyncState is RestCommandState)) { throw new Exception("Response Stream callback failed."); } RestCommandState reqState = (RestCommandState)ar.AsyncState; HttpWebRequest responseRequest = reqState.webRequest; HttpWebResponse response = (HttpWebResponse)responseRequest.EndGetResponse(ar); string responseMsg = new StreamReader(response.GetResponseStream()).ReadToEnd(); System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => GamePage.gamePage.DebugTextbox.Text = responseMsg); }