gpt4 book ai didi

c# - 带有 BeginGetRequestStream/EndGetRequestStream 的 TaskFactory.FromAsync 挂起

转载 作者:太空狗 更新时间:2023-10-30 00:15:30 24 4
gpt4 key购买 nike

我有一个这样的方法,它在使用 Task.Factory.FromAsync() 声明 responseObject 时挂起

private async Task<string> post(string url, string postdata)
{
var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
request.Method = "POST";

// this works
Task<Stream> requestStream = Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request);
var sw = new StreamWriter(requestStream.Result);
byte[] data = Encoding.UTF8.GetBytes(postdata);
await requestStream.Result.WriteAsync(data, 0, data.Length);

// this hangs
Task<WebResponse> responseObject = Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request); // Hangs here
// Doesn't get to here
var responseStream = responseObject.Result.GetResponseStream();
var sr = new StreamReader(responseStream);
string received = await sr.ReadToEndAsync();

return received;
}

如果我手动运行 Begin/End 方法,效果很好,如下所示:

        request.BeginGetRequestStream(async ar =>
{
var requestStream = request.EndGetRequestStream(ar);
using (var sw = new StreamWriter(requestStream))
{
byte[] data = Encoding.UTF8.GetBytes(postdata);
await requestStream.WriteAsync(data, 0, data.Length);
}

request.BeginGetResponse(async a =>
{
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(a);
var responseStream = response.GetResponseStream();
using (var sr = new StreamReader(responseStream))
{
string received = await sr.ReadToEndAsync();
}

}, null);
}, null);

但是,在那种情况下,ReadToEndAsync() 在单独的线程上运行,我希望该方法返回结果,这是不可能的,因为该方法在 BeginGetResponse 回调完成之前返回。

在这一点上,我认为我做的这件事非常错误,并且以完全错误的方式进行,因此非常感谢任何帮助。

最佳答案

首先:不要在异步方法中使用“.Result”。这将阻止正在运行您的方法的线程。请改用“await”,以便线程在获取结果时跳回到您的方法。

您的代码的问题在于您打开了一个请求流,但从未关闭它。那么它应该什么时候完成你的请求并发送呢?它总是期待更多的输入,直到请求超时运行。此外,您还没有设置内容类型和内容长度。

private async Task<string> post(string url, string postdata)
{
var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
request.Method = "POST";

byte[] data = Encoding.UTF8.GetBytes(postdata);
request.ContentLength = data.Length;

using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request))
{
await requestStream.WriteAsync(data, 0, data.Length);
}


WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
var responseStream = responseObject.GetResponseStream();
var sr = new StreamReader(responseStream);
string received = await sr.ReadToEndAsync();

return received;
}

关于c# - 带有 BeginGetRequestStream/EndGetRequestStream 的 TaskFactory.FromAsync 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14098258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com