【发布时间】:2013-09-09 13:58:17
【问题描述】:
我有以下代码使异步调用同步。
请不要认为这很糟糕,我知道,并且仅针对一个特定请求需要这样做。
在执行 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(字符串网址,字符串 requestData, Boolean isError, String filePath) at Gtas.Core.Helpers.GtasRequestWorker.HandleException(异常 异常,AppEnvironment appEnvironment,GtasPerformance GtasPerformance、LimitedCrashExtraDataList extraData、字符串文件路径) 在 Gtas_WP8.GtasHandler.UnhandledExceptionsHandler(对象发送者, ApplicationUnhandledExceptionEventArgs e) 在 MS.Internal.Error.CallApplicationUEHandler(异常 e)在 MS.Internal.Error.IsNonRecoverableUserException(异常前,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());
}
}
框架是否可能不允许这样做?
提前谢谢你。
【问题讨论】:
标签: c# exception windows-phone-8 httpwebrequest notsupportedexception