0

次のコードを使用して、Windows Phone 8 からサーバーにデータを送信しています。

responseString はデフォルト値になりつつあります。

ただし、数秒後に確認すると、更新されています。

リクエストがいつ完了したかを知る方法はありますか

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO;
using System.Text;

namespace Blood_Bank
{
    public partial class Page1 : PhoneApplicationPage
    {
        String responseString = "amit";
        public event Action Completed;
        StringBuilder postData = new StringBuilder();
        public Page1()
        {
            InitializeComponent();
        }


        private void submit_Click(object sender, RoutedEventArgs e)
        {
            // Create a new HttpWebRequest object
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.mywindowsproject.appspot.com");

            //   request.ContentType = "text/html";

            // Set the Method property to 'POST' to post data to the URI.
            request.Method = "POST";

            // start the asynchronous operation

            postData.Append("name=" + name.Text.ToString()+"&");


            request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);


                MessageBox.Show(responseString);         

        }
        private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {

            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation
            Stream postStream = request.EndGetRequestStream(asynchronousResult);


            // Convert the string into a byte array. 
            byte[] byteArray = Encoding.UTF8.GetBytes(postData.ToString());

            // Write to the request stream.
            postStream.Write(byteArray, 0, postData.Length);
            postStream.Close();

            // Start the asynchronous operation to get the response
            request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
        }
        private void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation

            try
            {


                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamRead = new StreamReader(streamResponse);
                responseString = streamRead.ReadToEnd();

                streamResponse.Close();
                streamRead.Close(); response.Close();
                if (Completed != null)
                    Completed();

            }
            catch (WebException e)
            {

            }
        }
    }
}
4

1 に答える 1

1

リクエストが完了したときに呼び出されるメソッドはすでに用意されています。メソッド GetResponseCallback では、更新された responseString が必要です。そのメソッドでイベントを発生させて、他のクラスが応答文字列の更新を処理したり、メソッド自体のロジックを処理したりできるようにすることができます。

このC# でのイベント ベースのプログラミングの概要も参照してください。

于 2013-06-25T08:53:17.403 に答える