我有以下代码可以使异步调用同步。
我知道,请不要认为这很糟糕,并且仅针对一个特定请求需要以这种方式完成。
当执行 using (var response = request.EndGetResponse(request.BeginGetResponse(null, null))) 时,代码在 Windows Phone 8 中引发异常。
该请求实际上是在 UnhandledException 事件处理程序中调用的。
例外:NotSupportedException
信息:Method is not supported
堆栈跟踪:
在 System.Net.Browser.ClientHttpWebRequest.BeginGetResponse(AsyncCallback 回调,对象状态) 在 Gtas.Core.ServiceRepository.ExecuteGtasRequest(String url, String requestData, Boolean isError, String filePath) 在 Gtas.Core.Helpers.GtasRequestWorker.HandleException(Exception异常, AppEnvironment appEnvironment, GtasPerformance GtasPerformance, LimitedCrashExtraDataList extraData, String filePath) at Gtas_WP8.GtasHandler.UnhandledExceptionsHandler(Object sender, ApplicationUnhandledExceptionEventArgs e) at MS.Internal.Error.CallApplicationUEHandler(Exception e) at MS.Internal.Error.IsNonRecoverableUserException(Exception ex , UInt32& xresultValue)
public void ExecuteRequest(string url, string requestData)
{
try
{
WebRequest request = WebRequest.Create(new Uri(url));
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers["Header-Key"] = "AKey";
// Convert the string into a byte array.
byte[] postBytes = Encoding.UTF8.GetBytes(requestData);
using (var requestStream = request.EndGetRequestStream(request.BeginGetRequestStream(null, null)))
{
// Write to the request stream.
endGetRequestStream.Write(postBytes, 0, postBytes.Length);
}
using (var response = request.EndGetResponse(request.BeginGetResponse(null, null))) // NotSupportedException
{
using (var streamRead = new StreamReader(response.GetResponseStream()))
{
// The Response
string responseString = streamRead.ReadToEnd();
if (!string.IsNullOrWhiteSpace(requestDataObjResult.FileName))
{
var fileRepo = new FileRepository();
fileRepo.Delete(request.FileName);
}
Debug.WriteLine("Response : {0}", responseString);
}
}
}
catch (WebException webEx)
{
WebExceptionStatus status = webEx.Status;
WebResponse responseEx = webEx.Response;
Debug.WriteLine(webEx.ToString());
}
}
框架是否有可能不允许这样做?
先感谢您。