将DateTime从Android发布到WCF RESTful JSON服务

Ham*_*eno 4 rest wcf datetime android android-json

我正在尝试将一个DateTime参数发送到通过JSON编码的WCF RESTful服务公开的方法.请求如下所示:

POST http://IP:PORT/LogService/json/GetLogEntriesByModule HTTP/1.1
Content-Length: 100
Content-Type: application/json
Host: IP:PORT
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
Expect: 100-Continue

{"maxentries":10,"upperdate":"1280703601462","lowerdate":"1277938801462","module":"Windows Service"}
Run Code Online (Sandbox Code Playgroud)

我尝试了几种格式DateTime:

  • 2010-07-01T10:54:00(由WCFTestClient应用程序通过NET.TCP 发送,它得到结果
  • \/Date(12345678+0100)\/
  • 01.07.2010 10:54:00

方法定义:

LogEntry[] GetLogEntriesByModule(
    string module,
    DateTime lowerDate,
    DateTime upperDate,
    int maxEntries,
    out bool maxEntriesReached
)
Run Code Online (Sandbox Code Playgroud)

我总是得到以下回应:

HTTP/1.1 200 OK
Content-Length: 60
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Fri, 02 Jul 2010 09:07:04 GMT

{"GetLogEntriesByModuleResult":[],"maxEntriesReached":false}
Run Code Online (Sandbox Code Playgroud)

似乎DateTime没有正确解析,因为Llog中有几个条目.

有谁知道如何做到这一点?

更新:问题出在服务器端并且已经解决.

小智 7

将日期发布到WCF服务的正确格式是使用: /Date(53244000000)/ 其中括号中的数字是自UTC UTC午夜以来的毫秒数.

Date dt = new Date();
long date = Date.UTC(dt.getYear(), dt.getMonth(), dt.getDay(), dt.getHours(),dt.getMinutes(), dt.getSeconds());
String senddate = "/date("+date+")/";
Run Code Online (Sandbox Code Playgroud)

然后使用它如下

inputparam.put("DateTime", datetime);
Run Code Online (Sandbox Code Playgroud)