Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
Cómo devolver una cadena desde asíncrono

Mi método es llamar a un servicio web y trabajar de forma asíncrona.

Al obtener respuesta, todo funciona bien y recibo mi respuesta.

El problema comienza cuando necesito devolver esta respuesta.

aquí está el código de mi método:

 public async Task<string> sendWithHttpClient(string requestUrl, string json) { try { Uri requestUri = new Uri(requestUrl); using (var client = new HttpClient()) { client.DefaultRequestHeaders.Clear(); ...//adding things to header and creating requestcontent var response = await client.PostAsync(requestUri, requestContent); if (response.IsSuccessStatusCode) { Debug.WriteLine("Success"); HttpContent stream = response.Content; //Task<string> data = stream.ReadAsStringAsync(); var data = await stream.ReadAsStringAsync(); Debug.WriteLine("data len: " + data.Length); Debug.WriteLine("data: " + data); return data; } else { Debug.WriteLine("Unsuccessful!"); Debug.WriteLine("response.StatusCode: " + response.StatusCode); Debug.WriteLine("response.ReasonPhrase: " + response.ReasonPhrase); HttpContent stream = response.Content; var data = await stream.ReadAsStringAsync(); return data; } } } catch (Exception ex) { Debug.WriteLine("ex: " + ex.Message); return null; }

y lo estoy llamando de esta manera:

 Task <string> result = wsUtils.sendWithHttpClient(fullReq, ""); Debug.WriteLine("result:: " + result);

pero al imprimir el resultado veo algo como esto: System.Threading.Tasks.Task

¿Cómo puedo obtener la cadena de resultados como lo hice con los datos dentro de mi método?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Debe hacer esto ya que está llamando al método async sincrónicamente :

 Task<string> result = wsUtils.sendWithHttpClient(fullReq, ""); Debug.WriteLine("result:: " + result.Result); // Call the Result

Piense en el tipo de retorno Task<string> como una 'promesa' de devolver un valor en el futuro.

Si llamó al método asíncrono de forma asíncrona, sería como lo siguiente:

 string result = await wsUtils.sendWithHttpClient(fullReq, ""); Debug.WriteLine("result:: " + result);
over 4 years ago · Santiago Trujillo Report

0

Un método asíncrono devuelve una tarea , que representa un valor futuro . Para obtener el valor real envuelto en esa tarea, debe await :

 string result = await wsUtils.sendWithHttpClient(fullReq, ""); Debug.WriteLine("result:: " + result);

Tenga en cuenta que esto requerirá que su método de llamada sea asíncrono. Esto es natural y correcto.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!