use*_*834 2 rest wcf json httpclient xamarin.forms
我有一个 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.
当我使用我的客户端联系 Web 服务时:我收到“错误请求”。
我寻找了一个解决方案,发现最好不要放置日期时间值,而是放置从 1970-01-01 开始的毫秒数。
我在 Postman 中使用了这个建议,并注意到创建一个新行效果很好。
邮递员请求的正文:
{
"Reservation_Utilisateur_Id" : "4",
"Reservation_Velo_Id" : "2",
"Reservation_DateDebut" : "\/Date(1245398693390)\/",
"Reservation_PeriodeDebut" : "matin",
"Reservation_DateFin" :"\/Date(1245398693390)\/",
"Reservation_PeriodeFin" : "matin"
}
Run Code Online (Sandbox Code Playgroud)
现在,我想知道如何将该对象发送到服务器。我的对象如何像上面那样序列化?
我寻找了一个没有成功的解决方案。
我不断收到“反序列化 BikeSharingService.Reservation 类型的对象时出错。DateTime 内容 '2016-08-22T00:00:00+02:00' 不以 '/Date(' 开头并以 ')/ 结尾' 根据 JSON 的要求。”
有人可以给新手我是一个解释,也许是一些有效的代码?
这是我的代码:
我的合同:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "create",
ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
Reservation create(Reservation reservation);
Run Code Online (Sandbox Code Playgroud)
服务方式:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在客户端:
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());
}
Run Code Online (Sandbox Code Playgroud)
然后在客户端调用方法:
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);
Run Code Online (Sandbox Code Playgroud)
我得到了什么:
假错误请求方法:POST,RequestUri:' http://localhost:51843/ServiceReservation.svc /create',版本:2.0,内容:System.Net.Http.StringContent,标题:{接受:应用程序/json Content-Type :应用程序/json;charset=utf-8
内容长度:407 } BadRequest 1.1反序列化 BikeSharingService.Reservation 类型的对象时出错。DateTime 内容 '2016-08-22T00:00:00+02:00' 不以 '/Date(' 开头并以 ')/' 结尾,这是 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)}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4162 次 |
| 最近记录: |