我正在使用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()
放在不同的类中,例如说classA
。现在我需要classB
中的httpresponse值。任何人都帮助我实现这一目标。
提前致谢
答案 0 :(得分: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)
{
}
}