2011-12-12 37 views
0

我試圖打電話與HttpWebRequest的WCF HttpWebRequest的Json的日期格式

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

一個WCF JSON服務這是我的電話

的日期值是DateTime.Now.Ticks

跟蹤告訴我「字符串未被識別爲有效的日期時間」。

我試過不逃避日期,但仍然是相同的結果。

回答

0

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(); 
    } 
} 
+0

我沒有任何格式的.ToString()。那沒用。猜猜我必須測試它是否可以通過。無論如何,這個工程.ToString(「yyyy-MM-ddThh:mm:ssZ」)。但thx爲您的幫助,我會標記爲awswer :-) – mimo