我是Xamarin的新手,试图构建一个简单的应用程序。我正在尝试发送一个简单的post呼叫,其中我发送一个参数。 这是我的代码:
private async Task fetchWwarrents(){
var request = new HttpRequestMessage(HttpMethod.Post,"http://perductor.hr/aplikacija/rest/radninalozi.php");
var keyValues = new List<KeyValuePair<string, string>>{
new KeyValuePair<string, string>("driver","1")
};
request.Content = new FormUrlEncodedContent(keyValues);
var client = new HttpClient();
var responseMessage = await client.SendAsync(request);
var jwt = await responseMessage.Content.ReadAsStringAsync();
}
它有效,我的意思是我从api获取数据,但无法识别驱动程序参数。我用邮递员测试了api,当我发布驱动程序参数时它正在工作..我不知道问题出在哪里,有人可以帮我吗?
答案 0 :(得分:2)
您发送的json请求格式错误,只需要修改Httpclient请求即可。
private async Task fetchWwarrents()
{
try
{
using (var client = new HttpClient())
{
HttpContent content=null;
var json = JsonConvert.SerializeObject(RquestString);
content = new StringContent(json, Encoding.UTF8, "application/json");
using (var response = await client.PostAsync("http://perductor.hr/aplikacija/rest/radninalozi.php", content))
{
var responseStr = await response.Content.ReadAsStringAsync();
if (!string.IsEmpty(responseStr))
{
//Parse the json string to object as per requirment
}
else
{
//Message
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine(@"ERROR {0}", ex.Message);
}
}