chanhle
chanhle

Reputation: 168

How to get responseString when make a post action window phone

I have class to create user and return the info of user (success).

    class POST
{
    public HttpWebRequest objRequest = null;
    //public static string myRequestData = string.Empty;
    public String myRequestData = String.Empty;
    public String urlAddress = "http://hackathon.kimhieu.info/flashcard/index.php/api/user";
    public  String responseString {get;set;}

    public void doSend()
    {
        StringBuilder completeUrl = new StringBuilder(urlAddress);
        objRequest = (HttpWebRequest)WebRequest.Create(urlAddress.ToString());
        objRequest.ContentType ="application/x-www-form-urlencoded";
        objRequest.Method = "POST";
        //Adding headers
        //objRequest.Headers["Header"]= "Your Value";
        //objRequest.Headers["Content-Language"] = "en-US";

        myRequestData = "username=abcdef&password=abcdef";

    //Begins the asynchronous request
    objRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),objRequest);


    }
    private  void GetRequestStreamCallback(IAsyncResult asyncResult)
      {
                HttpWebRequest objHttpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
                // End the operation
                Stream postStream = objHttpWebRequest.EndGetRequestStream(asyncResult);
                // Convert the string into a byte array.
                byte[] byteArray = Encoding.UTF8.GetBytes(myRequestData);
                // Write to the request stream.
                postStream.Write(byteArray, 0, myRequestData.Length);
                postStream.Close();

                // Start the asynchronous operation to get the response
                objHttpWebRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), objHttpWebRequest);
       }

    private void GetResponseCallback(IAsyncResult asyncResult)
            {
                HttpWebRequest objHttpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
                HttpWebResponse objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.EndGetResponse(asyncResult);
                Stream objStreamResponse = objHttpWebResponse .GetResponseStream();
                StreamReader objStreamReader = new StreamReader(objStreamResponse );
                responseString = objStreamReader.ReadToEnd();            // Got response here
                myRequestData = "AAA";
                //MessageBox.Show("RESPONSE :" + responseString);
                // Close the stream object
                objStreamResponse .Close();
                objStreamReader.Close();
                objHttpWebResponse.Close();
       }


}

I call in Main.xaml.cs

POST ab = new POST();
ab.doSend();                
MessageBox.Show(ab.responseString);

But It return Empty String I have try to assign some String in class POST myData but it not executed. I think GetResponseCallback(IAsyncResult asyncResult) not true. How can I fix it. Thank for advance !

Upvotes: 1

Views: 747

Answers (1)

igofed
igofed

Reputation: 1442

You write async code, but try to read responseString synchronously. Just add new event to your Post class:

public event Action Completed;

and run it from the end of method GetResponseCallback:

if(Completed != null)
   Completed();

and rewrite your code such way:

POST ab = new POST();
ab.Completed += () => { MessageBox.Show(ab.responseString); };
ab.doSend();   

It should work

Upvotes: 2

Related Questions