如何对HttpWebRequest异步调用
发布网友
发布时间:2022-08-20 06:42
我来回答
共1个回答
热心网友
时间:2023-11-01 15:07
如何对HttpWebRequest异步调用?
public static ManualResetEvent allDone = new ManualResetEvent(false);
static void Main(string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request);
allDone.WaitOne();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
streamResponse.Close();
streamRead.Close();
response.Close();
Console.WriteLine(responseString);
Console.ReadKey();