I am creating a mobile app using xamarin forms. However, I get the following exception:
Status code 500, ReasonPhrase:'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent,
While trying to execute postAsync
. I have no idea where it went wrong.
Here's the code that causes the exception:
private async void btnCheckout_OnClicked(object sender, EventArgs e)
{
ConstantCS constant = new ConstantCS();
var client = new HttpClient();
client.BaseAddress = new Uri(constant.URLBaseAddress);
//Store persive data locally
int MyOrdersID_ = int.Parse(Application.Current.Properties["MyOrdersID"].ToString());
for (int i = 0; i < orderItemList.Count; i++)
{
//string postdataJson = JsonConvert.SerializeObject(new { UserID = nric.Text.Trim(), Password = pin.Text.Trim() });
string postJson = JsonConvert.SerializeObject(new { orderID = MyOrdersID_, itemID = orderItemList[i].Item_Id, itemName = orderItemList[i].orderItem_Name, orderQty = orderItemList[i].orderItem_Quantity, itemRequest = orderItemList[i].orderItem_SpecialReq, specialRequest = orderItemList[i].orderItem_OtherReq , itemPrice = orderItemList[i].orderItem_IndividualPrice});
var content = new StringContent(postJson, Encoding.UTF8, "application/json");
//HttpResponseMessage orderItemResponse = await client.PostAsync("WebServices/Orders.svc/CreateOrderItems", content);
HttpResponseMessage orderItemResponse = new HttpResponseMessage();
orderItemResponse = client.PostAsync("WebServices/Orders.svc/CreateOrderItems", content).Result;
if (orderItemResponse.IsSuccessStatusCode)
{
await DisplayAlert("Success", "Order item response = " + orderItemResponse.ToString(), "OK");
}
else
{
await DisplayAlert("Failed", "Order item response = " + orderItemResponse.ToString(), "OK");
}
//var result = await response.Content.ReadAsStringAsync();
}
DateTime now = DateTime.Now;
string postOrderJson = JsonConvert.SerializeObject(new { custID = 1, orderDateTime = now, orderID = MyOrdersID_, orderTotalPrice = totalCost });
var OrderContent = new StringContent(postOrderJson, Encoding.UTF8, "application/json");
HttpResponseMessage orderResponse = await client.PostAsync("WebServices/Orders.svc/CreateOrder", OrderContent);
if (orderResponse.IsSuccessStatusCode)
{
await DisplayAlert("Success", "Order Response = " + orderResponse.ToString(), "OK");
}
else
{
await DisplayAlert("Failed", "Order Response = " + orderResponse.ToString(), "OK");
}
await Navigation.PushModalAsync(new MasterDetailPageFood());
//Display an alert and then go back to the first page
await DisplayAlert("Thank you for your order", "Your Order will be made shortly", "OK");
}
The error is on your web service. You should debug your web service and solve the problem there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With