gpt4 book ai didi

c# - 执行异步 Web 请求时出现问题

转载 作者:太空宇宙 更新时间:2023-11-03 14:31:13 25 4
gpt4 key购买 nike

谁能告诉我这段简单的代码做错了什么?当我运行它时,它会挂起

using (Stream postStream = request.EndGetRequestStream(asynchronousResult))

如果我注释掉 requestState.Wait.WaitOne();行代码正确执行但显然不等待响应。我猜对 EndGetRequestStream 的调用以某种方式使我返回到主线程的上下文?我很确定我的代码与示例基本相同 ( MSDN Documentation )

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Text;

namespace SBRemoteClient
{
public class JSONClient
{

public string ExecuteJSONQuery(string url, string query)
{
System.Uri uri = new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.Accept = "application/json";
byte[] requestBytes = Encoding.UTF8.GetBytes(query);
RequestState requestState = new RequestState(request, requestBytes);
IAsyncResult resultRequest = request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), requestState);
requestState.Wait.WaitOne();
IAsyncResult resultResponse = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(GetResponseStreamCallback), requestState);
requestState.Wait.WaitOne();

return requestState.Response;
}

private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
try
{
RequestState requestState = (RequestState)asynchronousResult.AsyncState;
HttpWebRequest request = requestState.Request;
using (Stream postStream = request.EndGetRequestStream(asynchronousResult))
{
postStream.Write(requestState.RequestBytes, 0, requestState.RequestBytes.Length);
}
requestState.Wait.Set();
}
catch (Exception e) {
Console.Out.WriteLine(e);
}
}

private static void GetResponseStreamCallback(IAsyncResult asynchronousResult)
{
RequestState requestState = (RequestState)asynchronousResult.AsyncState;
HttpWebRequest request = requestState.Request;
using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult))
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader streamRead = new StreamReader(responseStream))
{
requestState.Response = streamRead.ReadToEnd();
requestState.Wait.Set();
}
}
}
}
}
}

最佳答案

几件事:

  1. 我认为您不能重复使用同一个请求对象。也就是说,您可以为每个 HttpWebRequest 实例调用一次 BeginGetRequestStream。如果你想执行两个请求(调用服务器),你需要两个 HttpWebRequest 实例,每个请求一个。

  2. 如果您想要同步行为,您有两个选择:使用 GetRespose 方法或以同步方式使用开始/结束。为此,您不需要将回调传递给 BeginGetRequestStream 方法(您可以传递一个空值)。从 BeginGetRequestStream (IAsyncResult) 获取返回值并将其传递给 EndGetRequestStream 方法:

    AsyncResult resultRequest = request.BeginGetRequestStream(null, null);
    Stream postStream = request.EndGetRequestStream(asynchronousResult)

EndGetRequestStream 将阻塞直到请求完成(如果您从 UI 执行此操作很糟糕,但它仍然有效)。

关于c# - 执行异步 Web 请求时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2525775/

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