Skip to main content
1 de 2
novic
  • 36,7mil
  • 4
  • 32
  • 70

Tem 2 formas que conheço:

1)

private async void button1_Click(object sender, EventArgs e)
{
    await RequestJson();
    MessageBox.Show(retornojson);
}

private static string retornojson;
public async Task RequestJson()
{
    string req = "enviado";            
    string URL = "http://localhost/testjsonreturn.php";
    var myHttpClient = new HttpClient();
    var formContent = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("request", req)
        });
    var request = await myHttpClient.PostAsync(URL, formContent);
    retornojson = await request.Content.ReadAsStringAsync();            
    myHttpClient.Dispose();            
}

2)

private async void button1_Click(object sender, EventArgs e)
{
    retornojson = await RequestJson();
    MessageBox.Show(retornojson);
}

private static string retornojson;
public async Task<string> RequestJson()
{
    string req = "enviado";            
    string URL = "http://localhost/testjsonreturn.php";
    var myHttpClient = new HttpClient();
    var formContent = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("request", req)
        });
    var request = await myHttpClient.PostAsync(URL, formContent);
    return await request.Content.ReadAsStringAsync();                        
}
novic
  • 36,7mil
  • 4
  • 32
  • 70