0
I have a console application that receives data from a mobile device, I am trying to send this data to a web service to process the data and fill them in the database.
I’m trying this way:
public static async Task PostWS(string dado)
{
using (var httpClient = new HttpClient())
{
var url = @"rota do método no webservice";
var jsonString = @"{'Packet': '" + dado+"'}";
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
content.Headers.Clear();
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
HttpResponseMessage resposta = httpClient.PostAsync(url, content).Result;
if (resposta.IsSuccessStatusCode)
{
HttpContent conteudo = resposta.Content;
string resultado = conteudo.ToString();
Console.WriteLine("ok");
}
else
{
Console.WriteLine("erro");
}
}
Console.ReadKey();
}
I can access the method of my webservice the problem is that apparently I am not able to send the data correctly.
Webservice method I’m trying to access:
[HttpPost]
[Route("teste")]
public HttpResponseMessage testando(Device device)
{
string rout = ConfigurationManager.AppSettings["LogFile"];
try
{
System.IO.File.AppendAllText(@rout, device.Packet);
return Request.CreateResponse(HttpStatusCode.OK);
}
catch(Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
I cannot write the data in the text file. NOTE: No errors.
Where am I going wrong ?
Thanks in advance.
"it does not access the method of my webservice" can explain it better? gives error, which error? is a post even? the Contenttype is correct?
– Ricardo Pontual