我有一个 Visual Studio (2015) 项目,其中包括一个客户端部分 (Xamarin.Forms PCL) 和一个 Web 服务部分 (WCF Rest)。 Web 服务使用 edmx 与数据库通信(SQL Server 2016)。 JSON 用于交换数据。
我不熟悉创建/使用 WCF Rest 服务。我使用 GET 方法没有问题,但我遇到了 POST 方法的问题。
此方法是运行良好的服务的一部分:基于 GET 的方法没有问题。当我从 URL 或我的客户端 (PCL Xamarin.Forms) 对其进行测试时,它运行良好。
POST 方法(我的第一个方法)有点问题。
它应该在 SQL Server (2016) 的表中创建一条新记录。
当我使用 Postman ( https://www.getpostman.com/) 对其进行测试时,它已经存在一个问题:它在表中创建了一条记录,但该对象有两个日期,并且这两个日期被 1970-01-01 替换。
当我使用我的客户端联系网络服务时:我收到“错误请求”。
我找了一个解决方案,发现最好不要放置 Datetime 值,而是放置从 1970-01-01 开始的毫秒数。
我在 Postman 中使用了这个建议,并注意到新行的创建工作正常。
postman 请求的主体:
{
"Reservation_Utilisateur_Id" : "4",
"Reservation_Velo_Id" : "2",
"Reservation_DateDebut" : "\/Date(1245398693390)\/",
"Reservation_PeriodeDebut" : "matin",
"Reservation_DateFin" :"\/Date(1245398693390)\/",
"Reservation_PeriodeFin" : "matin"
}
现在,我想知道如何将该对象发送到服务器。如何像上面那样序列化我的对象?
我没有找到解决方案。
我不断收到“反序列化 BikeSharingService.Reservation 类型的对象时出错。DateTime 内容‘2016-08-22T00:00:00+02:00’不以‘/Date(’开头并以')/' 根据 JSON 的要求。”
有人可以给新手我是一个解释,也许还有一些有效的代码吗?
这是我的代码:
我的契约(Contract):
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "create",
ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
Reservation create(Reservation reservation);
服务方式:
public Reservation create(Reservation reservation)
{
using (MyEntities bse = new MyEntities())
{
Reservation re = new Reservation
{
Reservation_Utilisateur_Id = reservation.Reservation_Utilisateur_Id,
Reservation_Velo_Id = reservation.Reservation_Velo_Id,
Reservation_DateDebut = reservation.Reservation_DateDebut,
Reservation_PeriodeDebut = reservation.Reservation_PeriodeDebut,
Reservation_DateFin = reservation.Reservation_DateFin,
Reservation_PeriodeFin = reservation.Reservation_PeriodeFin,
Reservation_DemandeRecupDomCli = reservation.Reservation_DemandeRecupDomCli
};
bse.Reservations.Add(re);
bse.SaveChanges();
return re;
}
}
在客户端:
const string Url1 = "http://localhost:51843/ServiceReservation.svc/create";
public async Task<Reservation> create(Reservation reservation)
{
string json = JsonConvert.SerializeObject(reservation);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");
var response = await client.PostAsync(Url1,
new StringContent(
json,
Encoding.UTF8, "application/json"));
return JsonConvert.DeserializeObject<Reservation>(
await response.Content.ReadAsStringAsync());
}
然后在客户端调用方法:
Reservation re =new Reservation();
re.Reservation_Utilisateur_Id = 4;
re.Reservation_Velo_Id = 2;
re.Reservation_DateDebut = DateTime.Now.Date;
re.Reservation_PeriodeDebut = "matin";
re.Reservation_DateFin = DateTime.Now.Date;
re.Reservation_PeriodeFin = "matin";
re.Reservation_DemandeRecupDomCli = 1;
Reservation resultat = await reManager.create(re);
我得到的:
False Bad Request Method: POST, RequestUri: 'http://localhost:51843/ServiceReservation.svc /create', Version: 2.0, Content: System.Net.Http.StringContent, Headers: { Accept: application/json Content-Type: application/json; charset=utf-8
Content-Length: 407 } BadRequest 1.1There was an error deserializing the object of type BikeSharingService.Reservation. DateTime content '2016-08-22T00:00:00+02:00' does not start with '/Date(' and end with ')/' as required for JSON.
最佳答案
[从评论中提升]
Json 没有定义标准的日期格式,但值得注意的是 Json.Net(.Net 框架的大多数面向 Web 的部分都使用它)开箱即用地支持多种格式(甚至自定义个)。
如果您可以决定适用于所有客户端的标准,则可以在 .Net 中配置 Json(en/de)编码以在本地使用它。
参见 http://newtonsoft.com/json/help/html/datesinjson.htm有关如何指定日期格式处理程序的更多信息和详细信息。
[来自链接的示例代码]
public void WriteJsonDates()
{
LogEntry entry = new LogEntry
{
LogDate = new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc),
Details = "Application started."
};
// default as of Json.NET 4.5
string isoJson = JsonConvert.SerializeObject(entry);
// {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}
JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};
string microsoftJson = JsonConvert.SerializeObject(entry, microsoftDateFormatSettings);
// {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}
string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());
// {"Details":"Application started.","LogDate":new Date(1234656000000)}
}
关于json - Bad Request -Post 方法 - JSON DateTime 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39084723/