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

WP8 HttpWebRequest异步请求数据有关问题

2013-10-24 
WP8 HttpWebRequest异步请求数据问题刚接触WP8开发,请教大家一个关于HttpWebRequest异步请求数据的问题,定

WP8 HttpWebRequest异步请求数据问题
刚接触WP8开发,请教大家一个关于HttpWebRequest异步请求数据的问题,定义一个用户登录函数,函数列表中有输入和输出参数(ref),函数体就是异步Post请求数据和响应的过程,用的lamda表达式写法,想问的是在response中拿到返回数据后怎么给主函数通过ref输出?

private void UserLogin(string strin, ref string strout)
{
                //请求地址
                String url = "http://www.cnblogs.com/huizhang212/";
                //创建WebRequest类
                HttpWebRequest webRequest = HttpWebRequest.CreateHttp(new Uri(url));
                //设置请求方式GET POST
                webRequest.Method = "POST";
                //异步发起请求;
                webRequest.BeginGetRequestStream(hr =>
                {
                  HttpWebRequest httpRequest1 = (HttpWebRequest)hr.AsyncState;
                  System.IO.Stream requestStream =        httpRequest1.EndGetRequestStream(hr);
                  byte[] paramBytes = Encoding.UTF8.GetBytes(strin);
                  requestStream.Write(paramBytes, 0, paramBytes.Length);
                  requestStream.Close();

                  httpRequest1.BeginGetResponse(hr1 =>
                  {
                      WebResponse httpResponse1 = null;
                             try
                             {   httpResponse1 =  httpRequest1.EndGetResponse(hr1);
                                 using (var reader = new System.IO.StreamReader(httpResponse1.GetResponseStream(), System.Text.UTF8Encoding.UTF8))
                                 {
                                     strXMLResponse = reader.ReadToEnd(); 
                                     strout = strXMLResponse;//想在这里把返回值传给主函数输出,怎么办?
                                     reader.Close();
                                 }
                             }
                             catch (Exception ex)
                             {
                                 /*
                                 Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
                                 {


                                     MessageBox.Show(ex.Message);
                                 }));
                                  * */
                                 //return -1;
                             }
                             finally
                             {
                                 if (webRequest != null) webRequest.Abort();
                                 if (httpRequest1 != null) httpRequest1.Abort();
                                 if (httpResponse1 != null) httpResponse1.Close();
                                 //return -2;
                             }
                         }, httpRequest1);
                     }, webRequest);
}

热点排行