How to get async result in main thread - windows-phone-7

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();
}
}

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"));
});
}

Related

HttpWebRequest inside a custom class

I'm trying to make an app for windows phone 7 mango, to parse the content of a website. I managed to write all the code, but it was like a war zone ;). When i tried to rearrange the code in a better way, i start facing a very strange problem.
The problem is when i made a custom class called "MedinetMySchedule" inside my project and use breakpoint to step through this class. I found out that the app steps throught the first method getrequest() then return back to the mainpage for few steps, then returns back to the second methods GetRequestStreamCallback(). This jump to the mainpage leaves me with a string having a value of null to parse. Then it jump back to the third method ReadWebRequestCallback(). This last jump gives me nothing important as the debugg ends and i get nothing on the phone-emulator. The MedinetMySchedule class has the following code:-
namespace WindowsPhonePanoramaApplication1
{
public class MedinetMySchedule
{
//Medinet user url
public string url { get; set; }
public String myresponse;
public void getrequest()
{
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(GetRequestStreamCallback, request);
}
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
using (Stream postStream = request.EndGetRequestStream(asynchronousResult))
{
string postData = string.Format("username={0}&password={1}&customer=******&doLogin=Logga+in&language=se", "*****", "******");
// Convert the string into a byte array.
byte[] data = Encoding.UTF8.GetBytes(postData);
// Write to the request stream.
postStream.Write(data, 0, data.Length);
postStream.Close();
}
//Initiating get response
request.BeginGetResponse(ReadWebRequestCallBack, request);
}
private void ReadWebRequestCallBack(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
WebResponse myResponse = (WebResponse)request.EndGetResponse(asynchronousResult);
Stream encodingStream = myResponse.GetResponseStream();
Encoding encode = Encoding.GetEncoding("iso-8859-1");
using (StreamReader httpwebStreamReader = new StreamReader(encodingStream, encode))
{
myresponse= httpwebStreamReader.ReadToEnd();
}
myResponse.Close();
}
}
}
Here is the code that calls the getrequest() and parses the content:-
MedinetMySchedule mittschema = new MedinetMySchedule();
mittschema.url = "https://medinet.se/cgi-bin/doctor.pl?action=login&customer=saskir&language=se";
mittschema.getrequest();
Dispatcher.BeginInvoke(() => parseResults(mittschema.myresponse));
private void parseResults(string myresponse)
{
if (string.IsNullOrEmpty(myresponse))
{
return;
}
//Initiating a listbox and add item to it
List<MediNetScheme> medinetScheme = new List<MediNetScheme>();
//Using HtmlAgilityPack to parse the HTMLcode from the response
HtmlDocument htdoc = new HtmlDocument();
htdoc.LoadHtml(myresponse);
foreach (HtmlNode table in htdoc.DocumentNode.SelectNodes("//table[#class='list-medium']/tbody[1]/tr[#class]"))
{
//Removing ChildNode
table.ChildNodes.RemoveAt(3);
string itemValue = table.InnerText;
//Changing the parsed date into a DateTime
string d;
DateTime datum = DateTime.Parse(itemValue.Remove(11));
d = datum.ToString("D");
//Adding items to the listbox
medinetScheme.Add(new MediNetScheme() { Datum = d, Sections = itemValue.Remove(0, 15) });
}
MediNetScheme.ItemsSource = medinetScheme;
}
Any ideas why this is happening and how to correct it?
Yours
/Omar
The execution going back and forth is because request.BeginGetRequestStream is asynchronous. Basically, it creates a new thread, and executes in parallel of your main code. Therefore, you end up calling parseResult before you've finished downloading the data. There's many ways to rewrite your code, my favorite is using a callback:
First, change the getRequest method to accept a callback, and store it:
private Action Callback;
public void getrequest(Action callback)
{
this.Callback = callback;
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(GetRequestStreamCallback, request);
}
Then, at the end of ReadWebRequestCallback, invoke this callback:
private void ReadWebRequestCallBack(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
WebResponse myResponse = (WebResponse)request.EndGetResponse(asynchronousResult);
Stream encodingStream = myResponse.GetResponseStream();
Encoding encode = Encoding.GetEncoding("iso-8859-1");
using (StreamReader httpwebStreamReader = new StreamReader(encodingStream, encode))
{
myresponse= httpwebStreamReader.ReadToEnd();
}
myResponse.Close();
this.Callback();
}
Finally, from mainpage, call the getrequest method and tell it to use parseResults as callback:
Action callback = () => Dispatcher.BeginInvoke(() => parseResults(mittschema.myresponse));
mittschema.getrequest(callback);

binding bitmap image to a return stream from server after POST

I do a normal POST method and server return a stream, which is a bitmap image, then I want to set my Image1 to that stream that I got:
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
HttpStatusCode rcode = response.StatusCode;
Stream stream = response.GetResponseStream();
Dispatcher.BeginInvoke(() =>
{
var bi = new BitmapImage();
bi.SetSource(stream);
image1.Source = bi;
});
response.Close();
}
This give an error: Can not access a disposed object.
Object name: 'MS.Internal.InternalMemoryStream'.
I understand why I got this error, it's because the Stream response.Close(); and I use the Dispatcher. If I don't use Dispatcher it will give invalid-cross thread.
How do I set the stream to my image1?
Why don't you create a variable, which is avaliable both the callback and the UI thread?

how to make HTTP POST using reactive extension on windows phone 7

I found an example about HTTP POST in msdn, but I am wondering how can I make use of reactive extensions here.
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();
}
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();
Console.WriteLine(responseString);
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
allDone.Set();
}
}
I am trying to use the following code, but it does not work. Can anyone help me out on this?
Thanks in advance -Peng
return (from request in
Observable.Return((HttpWebRequest)WebRequest.Create(new Uri(postUrl))).Catch(Observable.Empty<HttpWebRequest>())
.Do(req =>
{
// Set up the request properties
req.Method = "POST";
req.ContentType = contentType;
req.UserAgent = userAgent;
req.CookieContainer = new CookieContainer();
Observable.FromAsyncPattern<Stream>(req.BeginGetRequestStream, req.EndGetRequestStream)()
.ObserveOnDispatcher()
.Subscribe(stream =>
{
stream.Write(formData, 0,
formData.Length);
stream.Close();
})
;
})
from response in
Observable.FromAsyncPattern<WebResponse>(request.BeginGetResponse, request.EndGetResponse)().Catch(Observable.Empty<WebResponse>())
from item in GetPostResponse(response.GetResponseStream()).ToObservable().Catch(Observable.Empty<string>())
select item).ObserveOnDispatcher();
Edit: To make it clear, I want to use the rx to implement the same logic in MSDN example.
in the MSDN example, it seems it first makes async call to write RequestStream, and then in the GetRequestStreamCallback, fires another async call to get the response.
Using Rx, I am able to create 2 observables
1. Observable.FromAsyncPattern(request.BeginGetRequestStream, request.EndGetRequestStream)()
2. Observable.FromAsyncPattern(request.BeginGetResponse, request.EndGetResponse)()
The problem is the second observable depends on the first one's result, so how can I do this in Rx?
In the first observable's subcribe method to create the seond observable? is it the good way?
This is how I am doing it. I configure the two Async patters up front, then use SelectMany to chain them together.
I have cut out the error handling etc from this code to keep it simple and show only the bare minimum to get it working. You should append a .Catch() similar to your own code, and if you want to get more than just a string out (say the response code) then you'll need to create a class/struct to hold all the bits of data you need and return that instead.
public IObservable<string> BeginPost(Uri uri, string postData) {
var request = HttpWebRequest.CreateHttp(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
var fetchRequestStream = Observable.FromAsyncPattern<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream);
var fetchResponse = Observable.FromAsyncPattern<WebResponse>(request.BeginGetResponse, request.EndGetResponse);
return fetchRequestStream().SelectMany(stream => {
using (var writer = new StreamWriter(stream)) writer.Write(postData);
return fetchResponse();
}).Select(result => {
var response = (HttpWebResponse)result;
string s = "";
if (response.StatusCode == HttpStatusCode.OK) {
using (var reader = new StreamReader(response.GetResponseStream())) s = reader.ReadToEnd();
}
return s;
});
}
Your problem is your use of Do() here, you need to move the GetRequestStream into your SelectMany (into your "from bla in, from bla in"...), since it only makes sense to get the response stream after you've written the full request. Right now, you're trying to do both concurrently.

httpWebRequest using POST method - receive unexpected xml response

I need to use POST to post a string to server and get xml response, the status code is OK but the string reponse is always ="" (0 byte). Is there any thing wrong with my code? I check the server from blackberry, works fine so the problem must come from my code:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
s = NavigationContext.QueryString["parameter1"];
//string strConnectUrl = "http://www.contoso.com/example.aspx";
base.OnNavigatedTo(e);
// DoWebClient(s);
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(strConnectUrl);
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
// start the asynchronous operation
httpWebRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), httpWebRequest);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
// string XML_REQUEST = "<?xml version=\"1.0\"?><mybroker"><getConnections></mybroker>";
string post = "?&track=love";
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
Stream postStream = request.EndGetRequestStream(asynchronousResult);
// Convert the string into a byte array.
byte[] postBytes = Encoding.UTF8.GetBytes(post);
// Write to the request stream.
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Close();
// Start the asynchronous operation to get the response
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
static Stream str;
static string st;
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
HttpStatusCode rcode = response.StatusCode;
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
//****THIS ALWAYS RETURN "" VALUE, EXPECT TO RETURN XML STRING****
string responseString = streamRead.ReadToEnd();
//Console.WriteLine(responseString);
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
}
*EDIT**
The code work, however the server return parameter require the Ampersand(&) which is not allow in silverlight framework I think, remove the & char server response but the result wasn't correct. I will ask a new question refer this Ampersand
Have checked your call using Fiddler. The server is returning an empty body. Your code is working correctly. The problem is server side.
The post value should be "track=love".
I tried and it worked but the response is gzip encoded.
Fiddler says that the request is good, and that the response really is blank. If it's working from Blackberry and not from WP7, could the server be doing some user agent checking and not returning anything because it doesn't recognize the WP7 user agent?
Also, it looks like you're POSTing a query string ("?&track=love") which is sort of unusual. Are you sure that's the right data to be sending in your request?

How to write an asynchronous method?

I have a method that performs Http POST, and since I'm using HttpWebRequest to perform it, the method relies on asynchronous calls. Since I need my method to return the response code of my Http POST, I want to make my method asynchronous. How do I do this?
I was thinking of using Dispatcher.
EDIT: So a basic outline of the structure of my code looks like this:
string response;
string httpPost(){
HttpWebRequest.BeginGetRequestStream(new AsyncCallback(requestCallback), httpWebRequest);
return response;
}
void requestCallback(IAsyncResult asyncResult){
HttpWebRequest.EndGetRequestStream(asyncResult);
HttpWebRequest.BeginGetResponse(new AsyncCallback(responseCallback), httpWebRequest);
}
void responseCallback(IAsyncResult asyncResult){
HttpWebResponse webResponse = (HttpWebResponse) HttpWebRequest.EndGetResponse(asyncResult);
response = webResponse.StatusCode.ToString();
}
I want to change httpPost() to an asynchronous method.
EDIT2:
public static void httpPost(Action<string> completed)
{
HttpWebRequest.BeginGetRequestStream(new AsyncCallback(requestCallback), httpWebRequest);
completed(HttpEngine.response);
}
On WP7, HTTPWebRequest will already be asynchronous - for an example of its use, see this code from http://www.rudigrobler.net/blog/wp7-webclient-vs-httpwebrequest
public void DoThePost(Action<string> onSuccess)
{
var request = (HttpWebRequest)WebRequest.Create(new Uri("http://www.sherdog.com/rss/news.xml"));
request.BeginGetResponse(r =>
{
var httpRequest = (HttpWebRequest)r.AsyncState;
var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(r);
using (var reader = new StreamReader(httpResponse.GetResponseStream()))
{
var response = reader.ReadToEnd();
Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
{
onSuccess(response);
}));
}
}, request);
}
Called with:
DoPost((responseText) => { responseTextBlock.Text = responseText;});

Resources