Question

I am using Windows Phone 7. How to get the response value from the following code???

private void sampleRequest()
{
    try
    {
        HttpWebRequest httpLoginRequest = (HttpWebRequest)WebRequest.Create(new Uri(DisplayMessage.Authentication_URL ));
        httpLoginRequest.Method = DisplayMessage.Get_Method;
        Parameters = new Dictionary<string, string>();
        httpLoginRequest.BeginGetResponse(new AsyncCallback(GetLoginCallback), httpLoginRequest);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

private void GetLoginCallback(IAsyncResult asynchronousResult)
{
    try
    {
        HttpWebRequest httpRequest = (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse httpresponse = (HttpWebResponse)httpRequest.EndGetResponse(asynchronousResult);
    }
    catch(WebException ex)
    {

    }
}

SampleRequest() is placed in different class for instance say classA. Now i need httpresponse value in classB. Anyone help me to achieve this.

Thanks in advance

Was it helpful?

Solution

There is several way for you to do that:

-The first way is for you to use the Microsoft.Bcl.Async nuget package, this package enable you to use async/await in a windows phone 7 application and will enable you to "just get the return the value" by awaiting sampleRequest in a similar way as you would be doing it if it was not asynchronous:

    private async Task<WebResponse> sampleRequest()
    {
        try
        {
            HttpWebRequest httpLoginRequest = (HttpWebRequest)WebRequest.Create(new Uri(DisplayMessage.Authentication_URL));
            httpLoginRequest.Method = DisplayMessage.Get_Method;
            Parameters = new Dictionary<string, string>();
            WebResponse webResponse =await httpLoginRequest.GetResponseAsync();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

That can be used in classB like this:

   WebResponse response=await classAInstance.sampleRequest()

-the second way is to pass your own callback as parameter of sampleRequest:

    public class State
    {
        public HttpWebRequest WebRequest { get; set; }
        public Action<HttpWebResponse > ResponseCallBack;
    }

    void sampleRequest(Action<HttpWebResponse > responseCallBack)
    {
        HttpWebRequest httpLoginRequest = (HttpWebRequest)WebRequest.Create(new Uri(DisplayMessage.Authentication_URL));
        //httpLoginRequest.Method = DisplayMessage.Get_Method;
        //Parameters = new Dictionary<string, string>();
        httpLoginRequest.BeginGetResponse(new AsyncCallback(GetLoginCallback), 
            new State(){WebRequest = httpLoginRequest,
                        ResponseCallBack = responseCallBack
            });
    }



    private void GetLoginCallback(IAsyncResult asynchronousResult)
    {
        try
        {
            State state= (State)asynchronousResult.AsyncState;
            HttpWebResponse httpresponse = (HttpWebResponse)state.WebRequest.EndGetResponse(asynchronousResult);
              ...

             //Dispatch request back to ui thread
              Deployment.Current.Dispatcher.BeginInvoke(() =>
              {
                   state.ResponseCallBack(httpresponse );
               });
        }
        catch(WebException ex)
        {

        }
    }

which you can use in classB like this:

classAInstance.sampleRequest((httpResponse)=>{
   //Do what you want with http response here
});

-the third way would be to just add an event on your classA that you will trigger in tGetLoginCallback and register for the event in classB:

   public event EventHandler<Response> LoginComplete;


   void sampleRequest(){
      //identical original one
      ....
    }


    private void GetLoginCallback(IAsyncResult asynchronousResult)
    {
        try
        {
            HttpWebRequest httpRequest = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse httpresponse = (HttpWebResponse)httpRequest.EndGetResponse(asynchronousResult);

            ...

              //Dispatch request back to ui thread
              Deployment.Current.Dispatcher.BeginInvoke(() =>
              {
                   if (LoginComplete != null)
                   {
                        LoginComplete(this,response);
                   }
               });

        }
        catch (WebException ex)
        {

        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top