首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Windows Mobile >

小弟我快疯了,关于wp http post 的有关问题

2013-06-26 
我快疯了,关于wp http post 的问题不管我做任何的处理或者不处理, 当调用 request.BeginGetResponse(new A

我快疯了,关于wp http post 的问题
不管我做任何的处理或者不处理, 当调用 
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), data);
线程就阻塞了,永远也没用办法触发 GetResponseCallback,即使我随便填了一个url,一般来说就算http请求时错误的,那也总得有个结果吧,代码;


        private ResultData HttpPost(MemoryStream stream)
        {
            try
            {
                stream.Position = 0;
                trace = Trace(stream);
                HttpWebRequest request = WebRequest.CreateHttp(ConfigurationManager.BaseUrl);
                request.ContentType = "application/json";
                request.Method = "POST";
                request.ContentLength = stream.Length;

                AsyncRequestData data = new AsyncRequestData()
                {
                    Request = request,
                    Data = stream
                };
                request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), data);
                _PostDoned.WaitOne();

                return Result;
            }
            catch (Exception ex)
            {
                throw new Exception("请求出错");
            }
        }
        private void GetRequestStreamCallback(IAsyncResult asyncResult)
        {
            try
            {
                AsyncRequestData asyncData = asyncResult.AsyncState as AsyncRequestData;
                HttpWebRequest request = asyncData.Request;
                MemoryStream mStream = asyncData.Data as MemoryStream;

                Stream httpStream = request.EndGetRequestStream(asyncResult);


                httpStream.Write(mStream.ToArray(), 0, (int)mStream.Length);
                httpStream.Close();

                AsyncRequestData data = new AsyncRequestData()
                {
                    Request = request,
                    Data = httpStream
                };
                
                request.BeginGetResponse(new AsyncCallback(GetResponseCallback), data);
                Thread.Sleep(100);
            }
            catch
            {
                throw new Exception("服务器无响应");
            }
        }
        private void GetResponseCallback(IAsyncResult asyncResult)
        {
            Stream httpStream = null;
            try
            {
                AsyncRequestData asyncData = asyncResult as AsyncRequestData;
                HttpWebRequest request = asyncData.Request;
                httpStream = asyncData.Data as Stream;

                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
                httpStream = response.GetResponseStream();

                DataContractJsonSerializer rspDataSerializer = new DataContractJsonSerializer(typeof(ResultData));
                Result = (ResultData)rspDataSerializer.ReadObject(httpStream);

                _PostDoned.Reset();
            }
            catch
            {
                throw new Exception("响应出错");


            }
            finally
            {
                if (httpStream != null)
                    httpStream.Close();
            }
        }

Http?Post
[解决办法]

public void PostResult(string url, string postParas)
        {
            HttpWebRequest request = HttpWebRequest.CreateHttp(url);
            request.Method = "POST";
            request.AllowReadStreamBuffering = true;
            
            try
            {
                request.BeginGetRequestStream((asyncResult) => 
                {
                    byte[] header = System.Text.Encoding.UTF8.GetBytes(postParas);

                    HttpWebRequest postRequest = asyncResult.AsyncState as HttpWebRequest;
                    Stream streamResult = postRequest.EndGetRequestStream(asyncResult);

                    //HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult);
                    //得到响应流
                    streamResult.Write(header, 0, header.Length);
                    streamResult.Flush();
                    streamResult.Close();
                    postRequest.BeginGetResponse((asyncPostResult) => 
                    {


                        HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;
                        HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult);
                        if (OnGetCompleted != null)
                        {
                            OnGetCompleted(this, webResponse.StatusCode.ToString());
                        }

                    }, postRequest);


                }, request);
                    
            }
            catch (WebException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


[解决办法]
引用:
Quote: 引用:

如果我没理解错的话你是想用异步的架构实现同步的效果是吧
我觉得这里windows phone的回调机制可能是通过队列实现的
也就是说回调函数也在主线程的函数队列中
这样的话前一个函数没执行完就永远不会执行到回调函数
个人猜测
仅供参考

其实你跟我想得一样,不过我没经验,而且就常识来讲异步应该就同多线程一样啊


httpwebrequest只有异步没有同步。如果要想在WP上做”同步“可以用async CTP 

热点排行