我正在使用xamarin表单创建移动应用。但是,我得到以下异常:
状态代码500,ReasonPhrase:'内部服务器错误',版本:1.1,内容:System.Net.Http.StreamContent,
尝试执行postAsync
时。我不知道它出了什么问题。
这是导致异常的代码:
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");
}
答案 0 :(得分:1)
错误在您的网络服务上。您应该调试您的Web服务并解决问题。