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」)。但thx爲您的幫助,我會標記爲awswer :-) – mimo