5

I'm making a login page on Windows Phone 7 app. I'd like to get login error status code on the login page when login error message return from server on async thread.

So my question is : In bellow code sample, please let me know how do you get "responseString(string)" in Main method?

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx

using System; using System.Net; using System.IO; using System.Text; using System.Threading; class HttpWebRequestBeginGetRequest { private static ManualResetEvent allDone = new ManualResetEvent(false); public static void Main(string[] args) { // Create a new HttpWebRequest object. HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.contoso.com/example.aspx"); request.ContentType = "application/x-www-form-urlencoded"; // Set the Method property to 'POST' to post data to the URI. request.Method = "POST"; // start the asynchronous operation request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request); // Keep the main thread from continuing while the asynchronous // operation completes. A real world application // could do something useful such as updating its user interface. allDone.WaitOne(); /* I'd like to get "responseString" here. */ } private static void GetRequestStreamCallback(IAsyncResult asynchronousResult) { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; // End the operation Stream postStream = request.EndGetRequestStream(asynchronousResult); Console.WriteLine("Please enter the input data to be posted:"); string postData = Console.ReadLine(); // Convert the string into a byte array. byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Write to the request stream. postStream.Write(byteArray, 0, postData.Length); postStream.Close(); // Start the asynchronous operation to get the response request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request); } private static void GetResponseCallback(IAsyncResult asynchronousResult) { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; // End the operation HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); Stream streamResponse = response.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse); string responseString = streamRead.ReadToEnd(); /* I'd like to get this responseString in Main method. */ Console.WriteLine(responseString); // Close the stream object streamResponse.Close(); streamRead.Close(); // Release the HttpWebResponse response.Close(); allDone.Set(); } } 

1 Answer 1

3

You could just define responseString as a class-level variable instead of defining it within the GetResponseCallback method. That way, it can be accessed from anywhere in the class, rather than just the method scope.

To navigate to another page from a background thread, you can use a Dispatcher.

 //Method to move to next page. Can be called from GetResponseCallBack private void NavigateToNextPage() { Dispatcher.BeginInvoke(() => { NavigationService.Navigate(new Uri("Page2.xaml", UriKind.Relative")); }); } 
Sign up to request clarification or add additional context in comments.

5 Comments

I'm sorry. I forgot to write that the code will hang in "allDone.WaitOne();". I don't know why but it seems to happen this issue if I use the code in Windows Phone app. So I can't use your advice.
@okame - Is there a reason you want to block the main thread, whilst using an async method? Also, are you getting a response successfully?
Yes. I want to stop to wait async thread. I wait async thead result, then going to next page if login succeeded.
There are two things that come to mind. Firstly, you can call a method that will navigate to the next page from within your GetResponseCallBack method. The navigation should be done on the UI thread (updated answer). Or, you could use a WebClient instead of HttpWebRequest since a WebClient will block the UI thread anyway.
Oh, that's a nice idea! I'll try it!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.