1

我正在使用 Windows Phone 7。如何从以下代码中获取响应值???

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()被放置在不同的类中,例如 say classA。现在我需要 httpresponse 值classB。任何人都可以帮助我实现这一目标。

提前致谢

4

1 回答 1

0

有几种方法可以做到这一点:

- 第一种方法是让您使用 Microsoft.Bcl.Async nuget 包,该包使您能够在 windows phone 7 应用程序中使用 async/await,并使您能够通过在中等待 sampleRequest 来“获取返回值”如果它不是异步的,那么你会这样做:

    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;
        }
    }

可以像这样在classB中使用:

   WebResponse response=await classAInstance.sampleRequest()

- 第二种方法是将您自己的回调作为 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)
        {

        }
    }

您可以像这样在 classB 中使用它:

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

- 第三种方法是在您的 classA 上添加一个事件,您将在 tGetLoginCallback 中触发该事件并在 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)
        {

        }
    }
于 2013-09-20T11:59:44.350 回答