silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了silverlight – HttpWebRequest.EndGetResponse在Windows Phone 7中抛出NotSupportedException异常大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

在Silverlight- Windows Phone 7项目中,我正在创建一个httpWebrequest,获取requestStream,写入Stream并尝试获取响应,但是我总是得到NotSupportedException: “System.Net.browser.oHWRAsyncResult.AsyncWaitHandle抛出了一个”System.NotSupportedExcepti
在Silverlight- Windows Phone 7项目中,我正在创建一个httpWebrequest,获取requestStream,写入Stream并尝试获取响应,但是我总是得到NotSupportedException:
“System.Net.browser.oHWRAsyncResult.AsyncWaitHandle抛出了一个”System.NotSupportedException“类型的异常

我的生产代码要复杂得多,但是我可以把它缩小到这个小代码

public class httpUploadHelper
{
    private httpWebrequest request;
    private requestState state = new requestState();

    public httpUploadHelper(String url)
    {
        this.request = Webrequest.Create(url) as httpWebrequest;
        state.request = request;
    }

    public void Execute()
    {
        request.Method = "POST";
        this.request.beginGetrequestStream(
            new AsyncCallBACk(Beginrequest),statE);
    }

    private void Beginrequest(IAsyncResult ar)
    {
        Stream stream = state.request.EndGetrequestStream(ar);
        state.request.beginGetResponse(
            new AsyncCallBACk(BeginResponsE),statE);
    }

    private void BeginResponse(IAsyncResult ar)
    {
        // BOOM: NotSupportedException was unhandled; 
        // {System.Net.browser.oHWRAsyncResult}
        // AsyncWaitHandle = 'ar.AsyncWaitHandle' threw an 
        // exception of type 'System.NotSupportedException'
        httpWebResponse response = state.request.EndGetResponse(ar) as httpWebResponse;
        Debug.WriteLine(response.StatusCodE);
    }
}

public class requestState
{
    public Webrequest request;
}

}

有人知道这段代码有什么问题吗?

解决方法@H_489_32@
调用EndGetResponse之前请求流未关闭时,可以抛出NotSupportedException异常.当您尝试获取响应时,Webrequest流仍然打开并将数据发送到服务器.由于流实现了IDisposable接口,一个简单的解决方案是使用使用块中的请求流来包装您的代码

private void Beginrequest(IAsyncResult ar)
{
    using (Stream stream = request.EndGetrequestStream(ar))
    {
        //write to stream in here.
    }
    state.request.beginGetResponse(
        new AsyncCallBACk(BeginResponsE),statE);
}

在尝试从Web服务器获取响应之前,使用块将确保流已关闭.

大佬总结

以上是大佬教程为你收集整理的silverlight – HttpWebRequest.EndGetResponse在Windows Phone 7中抛出NotSupportedException异常全部内容,希望文章能够帮你解决silverlight – HttpWebRequest.EndGetResponse在Windows Phone 7中抛出NotSupportedException异常所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。