gpt4 book ai didi

asp.net - 将参数传递给 BeginGetRequestStream

转载 作者:行者123 更新时间:2023-12-04 06:30:26 24 4
gpt4 key购买 nike

我正在使用线程池调用一些异步 Web 请求调用来发布数据

我在 for 循环中调用 RunWebAccess 函数

public void RunWebAccess (string strdara, int inttype)
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create("http://www.test.com");
byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(xmlvar);
req.Method = "POST";
req.ContentType = "text/xml";
req.ContentLength = requestBytes.Length;
IAsyncResult result =
req.BeginGetRequestStream(new AsyncCallback(ProcessResults), req);
}

private void ProcessResults(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
Stream postStream = request.EndGetRequestStream(result);
postStream.Write(result., 0, postData.Length ;
postStream.Close();
}

处理结果 函数不起作用,因为它无法访问参数。

问题是我想在 ProcessResults 函数中传递参数并写入流。

根据代码,我不能在 ProcessResults 函数中使用全局变量或读取输入。
(我想在 ProcessResults 函数中传递 strdara,inttype )

最佳答案

您可以对回调使用 lambda 表示法,如下所示:

public void RunWebAccess (string strdara, int inttype)
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create("http://www.test.com");
byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(xmlvar);
req.Method = "POST";
req.ContentType = "text/xml";
req.ContentLength = requestBytes.Length;
IAsyncResult result = null;
result =
req.BeginGetRequestStream(ar =>
{
Stream postStream = request.EndGetRequestStream(result);
postStream.Write(result., 0, postData.Length ;
postStream.Close();
}
, req);
}

关于asp.net - 将参数传递给 BeginGetRequestStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5482253/

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