【问题标题】:NotSupportedException thrown in EndGetResponse(request.BeginGetResponse(null, null)))在 EndGetResponse(request.BeginGetResponse(null, null))) 中抛出 NotSupportedException
【发布时间】: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


    【解决方案1】:

    您是否需要等到 BeginGetResponse 完成后再调用 EndGetResponse?这可能会导致 NotSupportedException。

    此外,我遇到了类似的问题。当我尝试使用 null 回调调用 BeginGetResponse 时引发了 NotSupportedException。我通过定义这个来解决这个问题

       (a)=>{}
    

    作为回调。 (那是银光)

    问候

    克里斯托夫

    【讨论】:

      猜你喜欢
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      相关资源
      最近更新 更多