错误的请求-Post方法-JSON日期时间问题

错误的请求-Post方法-JSON日期时间问题,json,rest,wcf,xamarin.forms,httpclient,Json,Rest,Wcf,Xamarin.forms,Httpclient,我有一个Visual Studio(2015)项目,其中包括一个客户端部分(Xamarin.Forms PCL)和一个web服务部分(WCF Rest)。web服务使用edmx与数据库通信(SQL Server 2016)。JSON用于交换数据 我不熟悉创建/使用WCF Rest服务。我在使用GET方法时没有问题,但是在使用POST方法时遇到了问题 此方法是工作良好的服务的一部分:基于GET的方法没有问题。当我从URL或我的客户端(PCL Xamarin.Forms)测试它时,它工作得很好 PO

我有一个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()测试它时,它已经有了一个问题:它在表中创建了一条记录,但该对象有两个日期,这两个日期被1970-01-01替换

当我使用我的客户机联系web服务时:我收到“坏请求”

我寻找了一个解决方案,发现与其放置Datetime值,不如放置1970-01-01之间的毫秒数

我在《邮递员》中使用了这个建议,并注意到创建一条新的线路效果很好

邮递员请求的正文:

{
"Reservation_Utilisateur_Id" : "4", 
"Reservation_Velo_Id" : "2",
"Reservation_DateDebut" : "\/Date(1245398693390)\/",
"Reservation_PeriodeDebut" : "matin",
"Reservation_DateFin" :"\/Date(1245398693390)\/", 
"Reservation_PeriodeFin" : "matin"
 }
现在,我想知道如何将该对象发送到服务器。如何像上面那样序列化我的对象

我找了一个解决办法,但没有成功

我不断得到“反序列化BikeSharegService.Reservation类型的对象时出错。DateTime内容“2016-08-22T00:00:00+02:00”不以JSON所需的“/Date(”和“)/”开头。”

有没有人能给新手一个解释,也许是一些代码

这是我的密码:

我的合同:

    [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);
我得到的是:

错误的请求方法:POST,请求URI: “/create”,版本:2.0, 内容:System.Net.Http.StringContent,标题:{接受: application/json内容类型:application/json;charset=utf-8
内容长度:407}BadRequest 1.1

反序列化类型的对象时出错 BikeSharegservice.Reservation。日期时间内容 “2016-08-22T00:00:00+02:00”不以“/Date(”开头,以“/Date(”结尾 JSON所需的“)/”


您尝试使用的是or

要将DateTime对象转换为历元DateTime,可以创建一个助手方法。从1970年1月1日起为毫秒或秒

如果需要,您还可以使用,而不是.NET,该.NET中包含要转换的方法

您可以为DateTime创建一个具有
string
数据类型的新类,并指定强制转换。或者,您也可以编写自定义序列化方法

默认情况下,序列化后的日期时间格式为

要转换为Unix或历元日期时间的代码:

private static readonly DateTime EpochDateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

public static long ConvertDateTimeToUnixTime(DateTime date, bool isDatarequiredInMilliSeconds = false, DateTimeKind dateTimeKind = DateTimeKind.Local)
{
     return Convert.ToInt64((DateTime.SpecifyKind(date.Value, dateTimeKind).ToUniversalTime() - EpochDateTime).TotalSeconds) * (isDatarequiredInMilliSeconds ? 1000 : 1);
}
如果需要转换回(),可以使用以下命令:


[从评论中提升]


Json没有定义标准的日期格式,但值得注意的是,Json.Net(被.Net framework中面向web的大多数部分使用)支持多种开箱即用格式(甚至是自定义格式)

如果您可以决定一个适用于所有客户机的标准,那么您可以在.Net中配置Json(en/de)编码以本机使用它

有关如何指定日期格式处理程序的更多信息和详细信息,请参阅

[链接中的示例代码]

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没有定义标准的日期格式,但值得注意的是,Json.Net(在.Net framework面向web的部分中经常使用)支持多种格式(甚至是自定义格式),因此如果您可以决定一种适用于所有客户机的标准,那么您可以配置Json(en/de)编码以在本机使用它。查看更多信息information@Basic非常感谢你。我使用了您建议的页面上提供的以下信息,它起了作用。实际上很简单:-)JsonSerializerSettings microsoftDateFormatSettings=new JsonSerializerSettings{DateFormatHandling=DateFormatHandling.MicrosoftDateFormat};字符串microsoftJson=JsonConvert.SerializeObject(条目,microsoftDateFormatSettings);感谢您抽出时间回答。事实上,我使用了一个JsonConverter,正如Basic所建议的那样,它很有效。不管怎样,谢谢你接听我的求助电话。
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)}
}