当我使用 HttpWebRequest 和 HttpWebResponse 时,我无法停止 UI 线程,当它调用 WaitOne() 时,它也会挂起

发布于 2024-11-17 11:50:10 字数 1777 浏览 3 评论 0原文

    void method1() 
    {
        url = "http://192.168.5.22/mobile/testpost.php" ;     
        request = (HttpWebRequest)WebRequest.Create(Url);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.AllowAutoRedirect = true;
        //// request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";

        RequestState myRequestState = new RequestState();
        myRequestState.Request = request;

        IAsyncResult result = request.BeginGetResponse(new AsyncCallback(FinaliseAsyncRequest), myRequestState);

        event_2.WaitOne();
        String s = myRequestState.responseAsString;
        String g = s;
    }

    private void FinaliseAsyncRequest(IAsyncResult AsyncResult)
    { 
        RequestState myrequestobj = (RequestState)AsyncResult.AsyncState;
        HttpWebRequest myrequest = myrequestobj.Request;
        response = (HttpWebResponse)myrequest.EndGetResponse(AsyncResult);
        string test1;
        test1 = ((HttpWebResponse)response).StatusDescription;
        if (response.StatusCode == HttpStatusCode.OK)
        {
            // Create the stream, encoder and reader.
            Stream responseStream = response.GetResponseStream();
            var x =response.GetResponseStream();
            //Encoding streamEncoder = Encoding.UTF8;
            StreamReader responseReader = new StreamReader(responseStream);
            responseAsString = responseReader.ReadToEnd();
            myrequestobj.responseAsString = responseAsString;
        }
        else
        {
            throw new Exception(String.Format("Response Not Valid {0}", response.StatusCode));
        }
        event_2.Set();
    }
    void method1() 
    {
        url = "http://192.168.5.22/mobile/testpost.php" ;     
        request = (HttpWebRequest)WebRequest.Create(Url);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.AllowAutoRedirect = true;
        //// request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";

        RequestState myRequestState = new RequestState();
        myRequestState.Request = request;

        IAsyncResult result = request.BeginGetResponse(new AsyncCallback(FinaliseAsyncRequest), myRequestState);

        event_2.WaitOne();
        String s = myRequestState.responseAsString;
        String g = s;
    }

    private void FinaliseAsyncRequest(IAsyncResult AsyncResult)
    { 
        RequestState myrequestobj = (RequestState)AsyncResult.AsyncState;
        HttpWebRequest myrequest = myrequestobj.Request;
        response = (HttpWebResponse)myrequest.EndGetResponse(AsyncResult);
        string test1;
        test1 = ((HttpWebResponse)response).StatusDescription;
        if (response.StatusCode == HttpStatusCode.OK)
        {
            // Create the stream, encoder and reader.
            Stream responseStream = response.GetResponseStream();
            var x =response.GetResponseStream();
            //Encoding streamEncoder = Encoding.UTF8;
            StreamReader responseReader = new StreamReader(responseStream);
            responseAsString = responseReader.ReadToEnd();
            myrequestobj.responseAsString = responseAsString;
        }
        else
        {
            throw new Exception(String.Format("Response Not Valid {0}", response.StatusCode));
        }
        event_2.Set();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城挽邺 2024-11-24 11:50:10

HttpWebRequest 方法似乎将其部分工作委托给 UI 线程。以下是我遇到的死锁情况的堆栈跟踪的一部分:

...
System.Windows.dll!System.Net.Browser.AsyncHelper.BeginOnUI(System.Net.Browser.BeginMethod beginMethod, System.AsyncCallback callback, object state) + 0x9b bytes
System.Windows.dll!System.Net.Browser.ClientHttpWebRequest.BeginGetResponse(System.AsyncCallback callback, object state) + 0x25 bytes
...

请注意 BeginGetResponse 之后的 BeginOnUI。 “异步”回调实际上与 UI 线程同步,因此当 UI 线程被阻塞时会挂起。 HttpWebResponse 方法可能会执行相同类型的同步。

HttpWebRequest methods seem to be delegating part of their work to the UI thread. Here is a part of a stack trace from a deadlock situation I had:

...
System.Windows.dll!System.Net.Browser.AsyncHelper.BeginOnUI(System.Net.Browser.BeginMethod beginMethod, System.AsyncCallback callback, object state) + 0x9b bytes
System.Windows.dll!System.Net.Browser.ClientHttpWebRequest.BeginGetResponse(System.AsyncCallback callback, object state) + 0x25 bytes
...

Note the BeginOnUI after the BeginGetResponse. The 'asynchronous' callback is actually synchronized with the UI thread and therefore hangs when the UI thread is blocked. HttpWebResponse methods might be doing the same kind of synchronization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文