刚接触WP8开发,请教大家一个关于HttpWebRequest异步请求数据的问题,定义一个用户登录函数,函数列表中有输入和输出参数(ref),函数体就是异步Post请求数据和响应的过程,用的lamda表达式写法,想问的是在response中拿到返回数据后怎么给主函数通过ref输出?
private void UserLogin(string strin, ref string strout)
{
//请求地址
String url = "http://www.cnblogs.com/huizhang212/";
//创建WebRequest类
HttpWebRequest webRequest = HttpWebRequest.CreateHttp(new Uri(url));
//设置请求方式GET POST
webRequest.Method = "POST";
//异步发起请求;
webRequest.BeginGetRequestStream(hr =>
{
HttpWebRequest httpRequest1 = (HttpWebRequest)hr.AsyncState;
System.IO.Stream requestStream = httpRequest1.EndGetRequestStream(hr);
byte[] paramBytes = Encoding.UTF8.GetBytes(strin);
requestStream.Write(paramBytes, 0, paramBytes.Length);
requestStream.Close();
httpRequest1.BeginGetResponse(hr1 =>
{
WebResponse httpResponse1 = null;
try
{ httpResponse1 = httpRequest1.EndGetResponse(hr1);
using (var reader = new System.IO.StreamReader(httpResponse1.GetResponseStream(), System.Text.UTF8Encoding.UTF8))
{
strXMLResponse = reader.ReadToEnd();
strout = strXMLResponse;//想在这里把返回值传给主函数输出,怎么办?
reader.Close();
}
}
catch (Exception ex)
{
/*
Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
{