I send in my app POST request to some URL. When I send it for the first time, it´s OK, but when I call again method SendBookingFreeCapacity(), then I get
ApplicationUnhandledExceptionEventArgs in App.xaml in function "private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)".
Problem is line:
webRequest.BeginGetResponse(new AsyncCallback(GetBookingFreeCapacitydResponseCallback), webRequest);
Here is my code:
public static void SendBookingFreeCapacity() {
var url = "https://..../getFreeCapacity";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.BeginGetRequestStream(
new AsyncCallback(GetBookingFreeCapacityRequestStreamCallback),
webRequest);
}
private static void GetBookingFreeCapacityRequestStreamCallback(
IAsyncResult asynchronousResult)
{
try {
HttpWebRequest webRequest =
(HttpWebRequest)asynchronousResult.AsyncState;
string postData = string.Empty;
System.IO.Stream postStream =
webRequest.EndGetRequestStream(asynchronousResult);
postData = "<?xml version=\"1.0\"?>"
+ "<request>"
+ "<login>" + Globals.Login + "</login>"
+ "<password>" + Globals.Password + "</password>"
+ "<hotId>" + htl.hotId + "</hotId>"
+ "<term>"
+ "<from>" + dayParser(Globals.PrijezdDate) + "</from>"
+ "<to>" + dayParser(Globals.OdjezdDate) + "</to>"
+ "</term>"
+ "</request>";
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
webRequest.BeginGetResponse(
new AsyncCallback(GetBookingFreeCapacitydResponseCallback),
webRequest); //after this I get the exception
}
catch (Exception ex) { }
}
private static void GetBookingFreeCapacitydResponseCallback(
IAsyncResult asynchronousResult)
{
try {
HttpWebRequest webRequest =
(HttpWebRequest)asynchronousResult.AsyncState;
//some code...
}
catch (Exception ex) { }
}
Do you have some tips what can cause it? Where is problem?
I founded the solution…create new page…