【问题标题】:WCF HttpWebRequest Json date formatWCF HttpWebRequest Json 日期格式
【发布时间】:2011-12-12 10:37:26
【问题描述】:

我正在尝试使用 HttpWebRequest 调用 WCF JSON 服务

http://localhost/WCFService/class/sessions?startdate=\\/Date({0})\\/&endDate=\\/Date({1})\\/

这是我的电话

日期的值为 DateTime.Now.Ticks

跟踪告诉我“字符串未被识别为有效的日期时间”。

我已尝试不逃避日期,但结果仍然相同。

【问题讨论】:

    标签: wcf json datetime serialization format


    【解决方案1】:

    URI 字符串中DateTime 值的格式是DateTime.Parse 可以识别的任何格式。 startDate.ToString("yyyy-MM-dd") 这样的东西效果很好。

    public class StackOverflow_8472985
    {
        [ServiceContract]
        public class Service
        {
            [WebGet]
            public int DiffDates(DateTime startDate, DateTime endDate)
            {
                Console.WriteLine("[service] startDate: {0}", startDate);
                Console.WriteLine("[service] endDate: {0}", endDate);
                return (int)endDate.Subtract(startDate).TotalDays;
            }
        }
        public static void Test()
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
            host.Open();
            Console.WriteLine("Host opened");
    
            WebClient c = new WebClient();
            DateTime startDate = new DateTime(2011, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            DateTime endDate = new DateTime(2011, 12, 12, 0, 0, 0, DateTimeKind.Utc);
    
            string uri = string.Format(baseAddress + "/DiffDates?startdate={0}&endDate={1}",
                startDate.ToString("yyyy-MM-dd"),
                endDate.ToString("yyyy-MM-dd"));
    
            Console.WriteLine("URI: {0}", uri);
            Console.WriteLine(c.DownloadString(uri));
    
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
    

    【讨论】:

    • 我做了一个没有任何格式的 .ToString() 。那没有用。猜猜我必须测试它是否可以通过。无论如何,这有效.ToString(“yyyy-MM-ddThh:mm:ssZ”)。但是谢谢你的帮助,我会标记为 awswer :-)
    猜你喜欢
    • 2011-12-22
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 2018-05-13
    • 2012-05-24
    • 1970-01-01
    相关资源
    最近更新 更多