[我正在尝试从xamarin.android应用程序中单击按钮以使用以下代码更新数据库中的记录时发送PUT请求:
我尝试使用邮递员发送此json数据,并且有效:
{'status': 'On'}
但是当我尝试在我的xamarin.android应用程序中发送它时,它不起作用。
错误:远程服务器返回错误(500)内部服务器错误
功能:
private async void SendAPIRequest(string method, string jsonrequest)
{
HttpWebRequest request = new HttpWebRequest(new Uri(String.Format("http://192.168.0.179:8080/api/devices/update/1")));//address of request
request.Method = "PUT";
request.ContentType = "application/json; charset=utf-8";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(jsonrequest);
streamWriter.Flush();
}
HttpWebResponse httpResponse = (HttpWebResponse)(await request.GetResponseAsync());
string json;
using (Stream responseStream = httpResponse.GetResponseStream())
{
json = new StreamReader(responseStream).ReadToEnd();//read the Json and assign it to a variable
}
}
按钮单击
private void BtnRequest_Click(object sender, EventArgs e)
{
string request = @"{'status': 'On'}";
SendAPIRequest("PUT", request);
}
PS:这只是一个测试API,因此不需要身份验证。
try
{
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
WebResponse wr = req.GetResponse();
}
catch (WebException wex)
{
var pageContent = new StreamReader(wex.Response.GetResponseStream())
.ReadToEnd();
}