2
I need that after making a requisition POST http, I also receive the json return that is in the echo of php.
This is the test code on php
<?php
if(isset($_POST['request']))
{
echo $jsonret = '{"request":"sim","name":"'.$_POST['request'].'"}';
}
else
{
echo $jsonret = '{"request":"não","name":"invalid"}';
}
?>
This was my attempt at C#, as asynchronous methods should always be of the void type, tried using a static variable, but even so, it does not change the string.
Observing: the request works without any problem.
private void button1_Click(object sender, EventArgs e)
{
RequestJson();
MessageBox.Show(retornojson);
}
static string retornojson = "nulo";
public async void RequestJson()
{
string req = "enviado";
//url do arquivo
string URL = "http://localhost/testjsonreturn";
var myHttpClient = new HttpClient();
//idusuario
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("request", req)
});
var request = await myHttpClient.PostAsync(URL, formContent);
retornojson = request.ToString();
}
Because of that statement:
devem ser sempre do tipo void
and variablestatic
is obliged because?– novic
Because the request can only be made asynchronously,
var request = await myHttpClient.PostAsync(URL, formContent);
– João Pedro
Pera a little you made a statement that the method can not be
void
and that’s not true? even what you went through is not void is with return! I know the solution to this problem, but, is with return? until I know withTask
also!– novic
You want the solution to this code?
– novic
Yes, it is possible by task to make the return, if you can give me the solution I thank you.
– João Pedro