没有什么好说的,直接上代码。
实体类:
public class NBOrder { /// <summary> /// 订单号 /// </summary> public string OrderCode { get; set; } /// <summary> /// 店铺名 /// </summary> public string ShopName { get; set; } /// <summary> /// 运费 /// </summary> public decimal WayFee { get; set; } /// <summary> /// 买家实付金额 /// </summary> public decimal PayAmount { get; set; } /// <summary> /// 购物网站买家账号 /// </summary> public string WebsiteAccount { get; set; } /// <summary> /// 手机号 /// </summary> public string Telephone { get; set; } /// <summary> /// 订单创建时间 /// </summary> public DateTime DateCreated { get; set; } /// <summary> /// 支付单号 /// </summary> public string PayCode { get; set; } /// <summary> /// 支付机构订单交易号 /// </summary> public string PaymentInstitutionPayCode { get; set; } /// <summary> /// 支付时间 /// </summary> public DateTime DatePaid { get; set; } /// <summary> /// 收件人名称 /// </summary> public string ReceiverName { get; set; } /// <summary> /// 收件人身份证号 /// </summary> public string ReceiverCardNo { get; set; } /// <summary> /// 收件人省 /// </summary> public string ReceiverProvince { get; set; } /// <summary> /// 收件人市 /// </summary> public string ReceiverCity { get; set; } /// <summary> /// 收件人区 /// </summary> public string ReceiverArea { get; set; } /// <summary> /// 收货人地址 /// </summary> public string ReceiverAddress { get; set; } /// <summary> /// 收货人电话 /// </summary> public string ReceiverMobile { get; set; } /// <summary> /// 收件人邮编 /// </summary> public string ReceiverPostCode { get; set; } /// <summary> /// 订单明细 /// </summary> public IEnumerable<NBOrderItem> OrderItems { get; set; } } public class NBOrderItem { /// <summary> /// 商品名称 /// </summary> public string ProductName { get; set; } /// <summary> /// 货号 /// </summary> public string ProductCode { get; set; } /// <summary> /// 数量 /// </summary> public int Quantity { get; set; } /// <summary> /// 商品单价 /// </summary> public decimal ProductPrice { get; set; } /// <summary> /// 商品金额 /// </summary> public decimal ProductAmount { get; set; } } public class OrderRequest { /// <summary> /// 账号名 /// </summary> public string AccountName { get; set; } /// <summary> /// 验证Key /// </summary> public string Key { get; set; } /// <summary> /// 推送订单数据 /// </summary> public NBOrder Order { get; set; } } public class OrderPushResult { /// <summary> /// 操作是否成功 /// </summary> public bool IsSuccess { get; set; } public string Message { get; set; } }
服务端代码:
// POST api/values public OrderPushResult Post([FromBody]OrderRequest value) { return new OrderPushResult() { IsSuccess = true, Message = "调用成功了。" }; }
客户端代码:
OrderRequest or = new OrderRequest(); or.AccountName = "NBTest"; or.Key = "4e0b859b68b3c9fdf59ebabf04aad8d2"; NBOrder no = new NBOrder(); no.OrderCode = "20160218153706565657"; no.ShopName = "测试店铺"; no.WayFee = 10; no.PayAmount = 40; no.WebsiteAccount = "zhangsan"; no.DateCreated = DateTime.Now; no.PayCode = "12312312312312312343"; no.PaymentInstitutionPayCode = "12312312312312312343"; no.DatePaid = DateTime.Now; no.ReceiverName = "张三"; no.ReceiverCardNo = "450102198109180032"; no.ReceiverProvince = "吉林省"; no.ReceiverCity = "长春市"; no.ReceiverArea = "其它区"; no.ReceiverAddress = "吉林省 长春市 其它区 吉林 长春市 其它区 汽车产业开发区中铁国际花园B2-204"; no.ReceiverMobile = "18504305117"; no.Telephone = "18504305117"; no.ReceiverPostCode = "422600"; List<NBOrderItem> OrderItems = new List<NBOrderItem>(); NBOrderItem item = new NBOrderItem(); item.ProductName = "尿不湿_销售"; item.ProductCode = "XSG9998"; item.Quantity = 1; item.ProductPrice = 20; item.ProductAmount = 20; OrderItems.Add(item); NBOrderItem item1 = new NBOrderItem(); item1.ProductName = "苹果_销售"; item1.ProductCode = "XSG9998"; item1.Quantity = 1; item1.ProductPrice = 20; item1.ProductAmount = 20; OrderItems.Add(item); no.OrderItems = OrderItems; or.Order = no; string json = JsonConvert.SerializeObject(or); HttpClient client = new HttpClient(); HttpContent contentPost = new StringContent(json, Encoding.UTF8, "application/json"); var response = client.PostAsync("WebAPI地址", contentPost).Result; //确保HTTP成功状态值 response.EnsureSuccessStatusCode(); //await异步读取最后的JSON string value = response.Content.ReadAsStringAsync().Result;