我有一個MVC行動.....WebRequest的MVC HttpPost日期時間格式
[HttpPost]
public ActionResult DoStuff(string myString, DateTime myDateTime)
...和我打電話從像這樣緊湊的框架應用程序的操作.....
WebRequest request = WebRequest.Create(url);
// Set the Method property of the request to POST.
request.Method = "POST";
request.Proxy = null;
// Create POST data and convert it to a byte array.
string postData = "myString=Bonjour&myDateTime=" + DateTime.Now.ToString();
byte[] byteArray = Encoding.UTF8.GetBytes(jsonPostData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
// Get the response.
using (WebResponse response = request.GetResponse())
{
// Display the status.
// Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
using (Stream responseStream = response.GetResponseStream())
{
// Read the response...
using (StreamReader reader = new StreamReader(responseStream))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}
問題是「myDateTime」參數始終爲空? postData字符串應該採用什麼格式才能工作(我已經嘗試了很多!)?
非常感謝,
ETFairfax
問題是日期時間格式中的空格。換成%20,一切都很好。 – ETFairfax 2011-01-19 00:13:09