【问题标题】:NotSupportedException on EndGetRequestStreamEndGetRequestStream 上的 NotSupportedException
【发布时间】:2014-04-27 02:33:30
【问题描述】:
public async static Task<WebResponse> GetResponseAsync(this HttpWebRequest request, Dictionary<string, object> post)
{
    var tcs = new TaskCompletionSource<WebResponse>();

    try
    {
        request.BeginGetRequestStream((arReq) =>
        {
            var stream = request.EndGetRequestStream(arReq);//throw NotSupportedException

            writeMultipartObject(stream, post);
            stream.Close();

            request.BeginGetResponse((ar) =>
            {
                var response = request.EndGetResponse(ar);
                tcs.SetResult(response);
            }, null);

        }, null);
    }
    catch (Exception we)
    {
        tcs.SetException(we);
    }

    return await tcs.Task;
}

当我发布一些东西时,它不起作用..=.= var stream = request.EndGetRequestStream(arReq);//抛出 NotSupportedException 告诉我为什么? ToT........

System.NotSupportedException ---> System.NotSupportedException:不支持指定的方法。 在 System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) 在 System.Net.Browser.ClientHttpWebRequest.c_DisplayClasse.b_d(Object sendState) 在 System.Net.Browser.AsyncHelper.c_DisplayClass1.b_0(Object sendState)

【问题讨论】:

  • 该方法类似于“GET”或“POST”,显然,服务器不支持您正在使用的方法,至少不支持该 URL。

标签: c# windows-phone-8


【解决方案1】:

我经历过类似的行为(仅在 Windows Phone 上)并通过显式处理您正在写入的流来使其正常工作。 所以尝试添加

stream.Flush(); 

之前,和 流.Dispose();

在你之后

stream.Close();

声明,看看是否有帮助。

显然,.Net 中网络堆栈的行为因您的代码运行所在的平台而异,因为 .Net 框架是针对每个平台“重新开发”的。

我希望这会有所帮助。

干杯, 克里斯托夫。

【讨论】:

  • 谢谢。这解决了我的一个类似问题。我收到 GetResponseAsync() 的此错误。根据您的提示,将流放在 using 块中解决了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
  • 1970-01-01
  • 2018-05-20
相关资源
最近更新 更多