代码之家  ›  专栏  ›  技术社区  ›  deanvmc

此Http请求的替代方案

  •  0
  • deanvmc  · 技术社区  · 14 年前

    我有一个类去获取一些数据并将其作为字符串返回。当这个对象工作时,有一个旋转图标让用户知道工作正在完成。问题是代码在响应返回之前退出。我坚持了一个

    while(response == null)
    

    response = (HttpWebResponse)request.EndGetResponse(AsyncResult);
    

    不是开火。它在控制台应用程序中触发ok,所以我将此归结为silverlight不喜欢的事情,下面是完整的代码:

    public class HttpWorker
    {
        private HttpWebRequest request;
        private HttpWebResponse response;
        private string responseAsString;
        private string url;
    
        public HttpWorker()
        {
    
        }
    
        public string ReadFromUrl(string Url)
        {
            url = Url;
            request = (HttpWebRequest)WebRequest.Create(url);
    
            request.CookieContainer = new CookieContainer();
            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";
    
            AsyncRequest(); // The Demon!
    
            return responseAsString;
        }
    
        private void AsyncRequest()
        {
            request.BeginGetResponse(new AsyncCallback(FinaliseAsyncRequest), null);
        }
    
        private void FinaliseAsyncRequest(IAsyncResult AsyncResult)
        {
            response = (HttpWebResponse)request.EndGetResponse(AsyncResult);
    
    
            if (response.StatusCode == HttpStatusCode.OK)
            {
                // Create the stream, encoder and reader.
                Stream responseStream = response.GetResponseStream();
                Encoding streamEncoder = Encoding.UTF8;
                StreamReader responseReader = new StreamReader(responseStream, streamEncoder);
                responseAsString = responseReader.ReadToEnd();
            }
            else
            {
                throw new Exception(String.Format("Response Not Valid {0}", response.StatusCode));
            }
        }
    
    }
    
    3 回复  |  直到 14 年前
        1
  •  1
  •   JSBÕ±Õ¸Õ£Õ¹    14 年前

    你是不是在忙着 (while response == null) 在UI线程上?的异步回调 HttpRequest 将在UI线程上传递,因此如果在同一线程上循环,则回调永远无法运行。您需要返回以允许主消息循环运行,然后将传递异步回调。

    上面的设计表明,无论如何,您真正需要的是同步获取。忘记回电话,直接打 FinaliseAsyncRequest ReadFromUrl 你自己。用户界面将一直挂起,直到请求完成,但听起来这就是您想要的。

        2
  •  1
  •   Mick N    14 年前

    我在这里发布了一个使用WebClient和HttpWebRequest的工作示例。

    WebClient, HttpWebRequest and the UI Thread on Windows Phone 7

    请随意重用代码。

        3
  •  0
  •   Community Mike Kinghan    7 年前

    从web服务器获取字符串的最简单方法是 WebClient.DownloadStringAsync() MSDN docs ).

    试试这样的:

    private void DownloadString(string address)
    {
        WebClient client = new WebClient();
        Uri uri = new Uri(address);
    
        client.DownloadStringCompleted += DownloadStringCallback;
        client.DownloadStringAsync(uri);
    
        StartWaitAnimation();
    }
    
    
    private void DownloadStringCallback(object sender, DownloadStringCompletedEventArgs e)
    {
        // Do something with e.Result (which is the returned string)
    
        StopWaitAnimation();
    }
    

    如果你真的要模仿同步行为,看看 Faking synchronous calls in Silverlight WP7