I have just started developing an application targeting windows phone 8.1. In my application I am calling my web service and getting results from it. But when i click the hardware back button while the web request is processing the page is redirecting to the previous page, but I couldn't stop processing the Web request. The result is getting displayed when it got the response.
My code is here:
private async void accAcctDet_Click(object sender, RoutedEventArgs e)
{
string selected = acct[cmbAccDetail.SelectedIndex];
string maskselect = cmbAccDetail.SelectedItem.ToString();
prog.Visibility = Visibility.Visible;
acctdetails obj = new acctdetails();
var result = await obj.GetToken(selected);
if (result.Substring(0, 2) == "Se" || result.Substring(0, 2) == "Ki")
{
prog.Visibility = Visibility.Collapsed;
MessageDialog msgbox = new MessageDialog("Service Unavailable. Please try again later", "e-Book!");
await msgbox.ShowAsync();
ContentRoot.Visibility = Visibility.Visible;
myCommandBar.Visibility = Visibility.Visible;
}
else
{
prog.Visibility = Visibility.Collapsed;
this.Frame.Navigate(typeof(acctdetinfo), result);
}
}
public async Task<string> GetToken(string selected)
{
string postData = "selAcct=" + selected;
var res = await post("myparameter", postData);
return res;
}
private async Task<string> post(string url, string postdata)
{
var request = WebRequest.Create(new Uri("myurl.com" + url)) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json";
byte[] data = Encoding.UTF8.GetBytes(postdata);
//request.ContentLength = data.Length;
using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request))
{
await requestStream.WriteAsync(data, 0, data.Length);
}
WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
var responseStream = responseObject.GetResponseStream();
var sr = new StreamReader(responseStream);
string received = await sr.ReadToEndAsync();
return received;
}
I need to stop the request once the user clicked the hardware back button.
Any help would be highly appreciated