0

保存从 WCF/web 服务接收到的 windows phone 中的数据。一段时间后可能会收到响应,因此如何处理这种情况。保存数据没问题,但是数据接收晚了怎么处理

4

2 回答 2

0

您可以使用此代码(显示我项目中的代码):

public void sendPost(string postData, Action<MyResponse, Exception> callback, CreateResponse creater)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(UrlRequest);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Accept = "application/json";
            webRequest.AllowAutoRedirect = true;
            webRequest.BeginGetRequestStream(new AsyncCallback(getRequestStreamCallback), new Request()
            {
                HttpRequest = webRequest,
                PostData = postData,
                Url = UrlRequest,
                CallBack = callback,
                Creater = creater
            });
        }

 private void getRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            var request = (Request)asynchronousResult.AsyncState;
            // End the stream request operation
            Stream postStream = request.HttpRequest.EndGetRequestStream(asynchronousResult);

            byte[] byteArray = Encoding.UTF8.GetBytes(request.PostData);

            // Add the post data to the web request
            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Close();

            // Start the web request
            request.HttpRequest.BeginGetResponse(new AsyncCallback(getResponseCallback), request);
        }

private void getResponseCallback(IAsyncResult asynchronousResult)
        {
            var request = (Request)asynchronousResult.AsyncState;
            try
            {

                HttpWebResponse response;

                // End the get response operation
                response = (HttpWebResponse)request.HttpRequest.EndGetResponse(asynchronousResult);
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(streamResponse);
                var myResponse = streamReader.ReadToEnd();
                streamResponse.Close();
                streamReader.Close();
                response.Close();
                MyResponse response_obj = request.Creater.CreateResponseObj();
                using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(myResponse)))
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(response_obj.GetType());
                    response_obj = (GYResponse)serializer.ReadObject(stream);
                    if (request.CallBack != null)
                    {
                        request.CallBack.Invoke(response_obj, null);
                    }
                }
            }
            catch (WebException e)
            {

                if (request.CallBack != null)
                {
                    request.CallBack.Invoke(null, e);
                }
            }
        }
public void getInfo(string uid, Action<MyResponse, Exception> callback)
        {
            CreateResponse creater = new CreateResponseGetInfo();
            string model = "User";
            string method = "getInfo";
            Params parametrs = new Params();
            parametrs.Uid = uid;
            //create yor request
            string request = getRequestString(model, method, parametrs, Atoken);                 
            sendPost(request, callback, creater);
        }

因此,您调用方法,该方法将请求发送到 Web 服务postRequester.getInfo(uid, ResponseHandler)并使用委托来处理结果。

private void ResponseHandler(MyResponse result, Exception error)
        {
            if (error != null)
            {
                string err = error.Message;
                return;
            }
            else
            {
                var infoResponse = result as ResponseGetInfo;
                if (infoResponse != null)
                {
                      //result processing..              
                }

            }
        }
于 2013-01-21T05:18:36.250 回答
0

您在 Windows Phone 应用程序中发出的所有 Web 请求都是异步的。这意味着,您从您的应用程序发出一个 Web 请求并附加一个处理程序来处理响应时的响应。在响应处理程序中,您必须处理响应并使用它做任何您想做的事情。

检查此链接使用 WebClient 和 HttpWebRequest

于 2013-01-21T06:19:23.210 回答