Windows Phone網路HttpWebRequest用法

l_serein發表於2012-11-28

在Windows Phone系統中,通過HttpWebRequest類可以很容易的傳送網路請求,獲取網路資料。HttpWebRequest是非同步操作,不會堵塞主執行緒。
  1.通過HttpWebRequest.CreateHttp()方法可以建立一個HttpWebRequest,下面程式碼簡單實現傳送一個GET請求。
  httpGet
 
        public void httpGet()
        {
            try
            {
                //請求地址
                String url = "http://www.2cto.com/";
                //建立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.2cto.com/";
                //建立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)
            {
                //異常處理

            }
        }
 

相關文章