我想通过 https 实现异步获取请求。这是分离 UI 和请求线程所必需的。这段代码可以找到,但我不确定它是否真的是异步的。请提供此代码的性能批评者。
public void authHttp()
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("uri");
request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
}
private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
{
string results = httpwebStreamReader.ReadToEnd();
//TextBlockResults.Text = results; //-- on another thread!
//Dispatcher.BeginInvoke(() => TextBlockResults.Text = results);
}
myResponse.Close();
}