方法异步的返回值

时间:2014-06-17 15:56:28

标签: c# json

我有一个主页,我在其中调用一个使用异步方法读取JSON的类,此方法效果很好,但我需要将JSON的值返回到主页面。

MainPage.cs,

private void btnPrueba_Click(object sender, RoutedEventArgs e)
{


Dictionary<string,string> parametros = new Dictionary<string,string>();
parametros.Add("login", "12345678R");
parametros.Add("password", "123123");

objPrueba = new prueba();

objPrueba.doPost("http://prueba.es/login", parametros);

}

Class Post.cs,

public void doPost(string url, Dictionary<string, string> parametros)
{
System.Uri myUri = new System.Uri(url);
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";

//relleno variable con los parametros
foreach (KeyValuePair<string, string> D in parametros)
{
parametrosData = parametrosData + D.Key + "=";
parametrosData = parametrosData + D.Value + "&";
}


bool a = myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest).IsCompleted;

string b = "1231232";


}


void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
// End the stream request operation
Stream postStream = myRequest.EndGetRequestStream(callbackResult);

// Create the post data
string postData = parametrosData;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();

// Start the web request




myRequest.BeginGetResponse(new System.AsyncCallback(GetResponsetStreamCallback), myRequest);
}

void GetResponsetStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackRe sult);
using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
{
string result = httpWebStreamReader.ReadToEnd();
//For debug: show results
Debug.WriteLine(result);

//RETURN VALUE result TO MAINPAGE.CS



}
}

1 个答案:

答案 0 :(得分:0)

您最好使用异步函数,这会使您的代码更简单。

你会有类似的东西:(抱歉格式错误,我对StackOverflow编辑器还不熟悉......)

public async Task<string> doPost(string url, Dictionary<string, string> parametros)
{

System.Uri myUri = new System.Uri(url);
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";

//relleno variable con los parametros
foreach (KeyValuePair<string, string> D in parametros)
{
parametrosData = parametrosData + D.Key + "=";
parametrosData = parametrosData + D.Value + "&";
}

Stream postStream = await myRequest.GetRequestStreamAsync();

// Create the post data
string postData = parametrosData;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();

// Start the web request

HttpWebResponse response = await myRequest.GetResponseAsync();

string result;

using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
{
 result = httpWebStreamReader.ReadToEnd();


//For debug: show results
Debug.WriteLine(result);
}

return result;
}

然后你就可以做到:

private async void btnPrueba_Click(object sender, RoutedEventArgs e)
{


Dictionary<string,string> parametros = new Dictionary<string,string>();
parametros.Add("login", "12345678R");
parametros.Add("password", "123123");

objPrueba = new prueba();

string result = await objPrueba.doPost("http://prueba.es/login", parametros);

// You can use the string to update the GUI if you want, because this code is running on the GUI thread

}