我将我的应用程序 POST 请求发送到某个 URL。当我第一次发送它时,没关系,但是当我再次调用方法时SendBookingFreeCapacity()
,我得到了
ApplicationUnhandledExceptionEventArgs
在 App.xaml 中的函数中"private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)"
。
问题是行:
webRequest.BeginGetResponse(new AsyncCallback(GetBookingFreeCapacitydResponseCallback), webRequest);
这是我的代码:
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) { }
}
你有什么提示是什么导致的吗?问题出在哪里?