Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从回调函数wp7获取响应值_C#_Windows Phone 7_Httpwebrequest_Httpwebresponse - Fatal编程技术网

C# 如何从回调函数wp7获取响应值

C# 如何从回调函数wp7获取响应值,c#,windows-phone-7,httpwebrequest,httpwebresponse,C#,Windows Phone 7,Httpwebrequest,Httpwebresponse,我正在使用WindowsPhone7。如何从以下代码中获取响应值 private void sampleRequest() { try { HttpWebRequest httpLoginRequest = (HttpWebRequest)WebRequest.Create(new Uri(DisplayMessage.Authentication_URL )); httpLoginRequest.Method = DisplayMessage.G

我正在使用WindowsPhone7。如何从以下代码中获取响应值

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


提前感谢

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

-第一种方法是使用Microsoft.Bcl.Async nuget软件包,该软件包使您能够在windows phone 7应用程序中使用Async/await,并使您能够通过等待sampleRequest来获取返回值,方法与在非异步情况下执行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;
        }
    }
-第二种方法是将自己的回调作为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)
        {

        }
    }
-第三种方法是只在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)
        {

        }
    }

你试过什么吗。你可以尝试将响应公开,并在类b中声明类A,这样你就可以访问那里的响应。如果我也公开,我就不会得到值。。。因为它是一个回调函数,它将单独运行……哦,我明白了,我之前错过了:你能告诉我你在哪里声明了myresponse吗。在第二种方式中,我只是一个占位符,用于传递回类B的任何内容,一般来说,它只是一个类的实例,在这个实例中,您将提取任何您想要返回的数据。在我的示例中,这个类称为Response,它是我在State中使用的。例如,在登录的情况下,OAuth令牌maybe?in func响应不可用,请求namespace.ya对。。。所以,只有我试图通过httpresponse,但它说func不需要1个参数。用httpresponse更新了我的答案,而且需要使用的是Action而不是func
classAInstance.sampleRequest((httpResponse)=>{
   //Do what you want with http response here
});
   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)
        {

        }
    }