Windows Phone 网络HttpWebRequest用法

原文地址:http://www.cnblogs.com/huizhang212/archive/2012/05/17/HttpWebRequest.html

 

 

在Windows Phone系统中,通过HttpWebRequest类可以很容易的发送网络请求,获取网络数据。HttpWebRequest是异步操作,不会堵塞主线程。
  1.通过HttpWebRequest.CreateHttp()方法可以创建一个HttpWebRequest,下面代码简单实现发送一个GET请求。
httpGet
复制代码
        public void httpGet()

        {

            try

            {

                //请求地址

                String url = "http://www.cnblogs.com/huizhang212/";

                //创建WebRequest类

                HttpWebRequest request = HttpWebRequest.CreateHttp(new Uri(url));



                //设置请求方式GET POST

                request.Method = "GET";



                //返回应答请求异步操作的状态

                request.BeginGetResponse(responseCallback, request);

            }

            catch (WebException e)

            {

                //网络相关异常处理

            }

            catch (Exception e)

            {

                //异常处理

            }

        }
复制代码

  2.应答数据接收部分。

responseCallback
复制代码
        private void responseCallback(IAsyncResult result)

        {

            try

            {

                //获取异步操作返回的的信息

                HttpWebRequest request = (HttpWebRequest)result.AsyncState;

                //结束对 Internet 资源的异步请求

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

                //解析应答头

                //parseRecvHeader(response.Headers);

                //获取请求体信息长度

                long contentLength = response.ContentLength;



                //获取应答码

                int statusCode = (int)response.StatusCode;

                string statusText = response.StatusDescription;



                //应答头信息验证

                using (Stream stream = response.GetResponseStream())

                {

                    //获取请求信息

                    StreamReader read = new StreamReader(stream);

                    string msg = read.ReadToEnd();

                    Deployment.Current.Dispatcher.BeginInvoke(() =>

                    {

                        textBlock1.Text = msg;

                    });

                }



            }

            catch (WebException e)

            {

                //连接失败

               

            }

            catch (Exception e)

            {

                //异常处理

                

            }



        }
复制代码

  通过HttpWebResponse可以获取返回的数据,在获取数据后,要想将数据显示到界面中,这里要主要一个问题。由于HttpWebRequest是异步操作,所以这里应该有一个线程来处理网络,大家都知道Windows Phone中在线程里是不能操作UI的,这个需要交个UI主线程来处理,所以代码中用到了Deployment.Current.Dispatcher.BeginInvoke。
  3.上面是简单的GET请求,POST请求和GET请求相比,多了一个发送请求体的操作过程。以下代码为POST请求,应对部分操作函数仍然是responseCallback。

httpPost
复制代码
        public void httpPost()

        {

            try

            {

                //请求地址

                String url = "http://www.cnblogs.com/huizhang212/";

                //创建WebRequest类

                HttpWebRequest request = HttpWebRequest.CreateHttp(new Uri(url));



                //设置请求方式GET POST

                request.Method = "POST";



                //返回应答请求异步操作的状态

                request.BeginGetRequestStream(requestCallback, request);

            }

            catch (WebException e)

            {

                //网络相关异常处理

            }

            catch (Exception e)

            {

                //异常处理

            }

        }



        private void requestCallback(IAsyncResult result)

        {

            try

            {

                //获取异步操作返回的的信息

                HttpWebRequest request = (HttpWebRequest)result.AsyncState;

                //结束对 Internet 资源的异步请求

                StreamWriter postStream = new StreamWriter(request.EndGetRequestStream(result));

                postStream.WriteLine("作者:宇之乐");

                postStream.WriteLine("出处:http://www.cnblogs.com/huizhang212/");



                //返回应答请求异步操作的状态

                request.BeginGetResponse(responseCallback, request);

            }

            catch (WebException e)

            {

                //异常处理



            }

            catch (Exception e)

            {

                //异常处理



            }

        }

你可能感兴趣的:(windows phone)