欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Windows Phone网络HttpWebRequest用法

程序员文章站 2022-09-17 21:46:36
 在Windows Phone系统中,通过HttpWebRequest类可以很容易的发送网络请求,获取网络数据。HttpWebRequest是异步操作,不会堵塞主线程。   1....

 在Windows Phone系统中,通过HttpWebRequest类可以很容易的发送网络请求,获取网络数据。HttpWebRequest是异步操作,不会堵塞主线程。
  1.通过HttpWebRequest.CreateHttp()方法可以创建一个HttpWebRequest,下面代码简单实现发送一个GET请求。
  httpGet
 
        public void httpGet()
        {
            try
            {
                //请求地址
                String url = "/";
                //创建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 = "/";
                //创建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("");

                //返回应答请求异步操作的状态
                request.BeginGetResponse(responseCallback, request);
            }
            catch (WebException e)
            {
                //异常处理

            }
            catch (Exception e)
            {
                //异常处理

            }
        }
 
 


作者:宇之乐