c# – HttpWebRequest.BeginGetResponse

c# – HttpWebRequest.BeginGetResponse,第1张

概述我需要向Web资源发出异步请求并使用此页面中的示例( link to full example): HttpWebRequest myHttpWebRequest= (HttpWebRequest)WebRequest.Create("http://www.contoso.com");RequestState myRequestState = new RequestState(); myRe 我需要向Web资源发出异步请求并使用此页面中的示例( link to full example):
httpWebRequest myhttpWebRequest= (httpWebRequest)WebRequest.Create("http://www.contoso.com");RequestState myRequestState = new RequestState();  myRequestState.request = myhttpWebRequest;// Start the asynchronous request.IAsyncResult result=        (IAsyncResult) myhttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallback),myRequestState);

但是当我测试应用程序时,执行冻结(在2-3秒内)在此代码的最后一行(我可以使用调试器观察它).

为什么?这是我的错误还是函数的标准行为?

解决方法 你可以试试,我相信那更好
private voID StartWebRequest(string url){   httpWebRequest request = (httpWebRequest)WebRequest.Create(url);   request.BeginGetResponse(new AsyncCallback(FinishWebRequest),request);}private voID FinishWebRequest(IAsyncResult result){   httpWebResponse response = (result.AsyncState as httpWebRequest).EndGetResponse(result) as httpWebResponse;}

因为textBox’值的chross线程,但是这是wpf应用程序我会重拍这个,顺便说一下你可以使用webclIEnt之类的

private voID tbWord_TextChanged(object sender,TextChangedEventArgs e)    {        WebClIEnt wc = new WebClIEnt();        wc.DownloadStringCompleted += httpsCompleted;        wc.DownloadStringAsync(new Uri("http://en.wikipedia.org/w/API.PHP?action=opensearch&search=" + tbWord.Text));     }    private voID httpsCompleted(object sender,DownloadStringCompletedEventArgs e)    {        if (e.Error == null)        {             //do what ever              //with using e.Result        }    }
总结

以上是内存溢出为你收集整理的c# – HttpWebRequest.BeginGetResponse全部内容,希望文章能够帮你解决c# – HttpWebRequest.BeginGetResponse所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1248367.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存