我有一个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