Skip to main content
adicionou 249 caracteres ao conteúdo
Fonte Link
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)ou

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();                        
}

Tinha alguns problemas o endereço por exemplo, faltou .php e quando o processo de requisição realmente era concretizado, faltou 1 linha de código:

retornojson = await request.Content.ReadAsStringAsync();

para ler o contéudo enviado pelo PHP.

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();                        
}

Tem 2 formas que conheço:

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();            
}

ou

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();                        
}

Tinha alguns problemas o endereço por exemplo, faltou .php e quando o processo de requisição realmente era concretizado, faltou 1 linha de código:

retornojson = await request.Content.ReadAsStringAsync();

para ler o contéudo enviado pelo PHP.

Fonte Link
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();                        
}