HttpWebRequest.EndGetResponse在Windows Phone 7中引发NotSupportedException [英] HttpWebRequest.EndGetResponse throws a NotSupportedException in Windows Phone 7

查看:119
本文介绍了HttpWebRequest.EndGetResponse在Windows Phone 7中引发NotSupportedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Silverlight-Windows Phone 7项目中,我正在创建HttpWebRequest,获取RequestStream,将某些内容写入Stream中并尝试获取响应,但是我始终会收到NotSupportedException: "System.Net.Browser.OHWRAsyncResult.AsyncWaitHandle引发了类型为'System.NotSupportedException'的异常

in a Silverlight-Windows Phone 7-project I am creating an HttpWebRequest, get the RequestStream, write something into the Stream and try to get the response, but I always get a NotSupportedException: "System.Net.Browser.OHWRAsyncResult.AsyncWaitHandle threw an exception of type 'System.NotSupportedException'

我的生产代码要复杂得多,但是我可以将其范围缩小到这小段代码:

My production code is far more complicated, but I can narrow it down to this small piece of code:

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;
}

}

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

Does anybody know what is wrong with this code?

推荐答案

在调用EndGetResponse之前未关闭请求流时,可以引发NotSupportedException.尝试获取响应时,WebRequest流仍处于打开状态,并且正在将数据发送到服务器.由于流实现了IDisposable接口,一个简单的解决方案是使用请求流将代码包装在using块中:

The NotSupportedException can be thrown when the request stream isn't closed before the call to EndGetResponse. The WebRequest stream is still open and sending data to the server when you're attempting to get the response. Since stream implements the IDisposable interface, a simple solution is to wrap your code using the request stream in a using block:

private void BeginRequest(IAsyncResult ar)
{
    using (Stream stream = request.EndGetRequestStream(ar))
    {
        //write to stream in here.
    }
    state.Request.BeginGetResponse(
        new AsyncCallback(BeginResponse), state);
}

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

The using block will ensure that the stream is closed before you attempt to get the response from the web server.

这篇关于HttpWebRequest.EndGetResponse在Windows Phone 7中引发NotSupportedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆