我目前正在为一些在线业务开发一个网络表单。HTTPWebrequest 有很大的问题。函数 UpdateUserData() 有一些典型的数据库更新信息。姓名,电子邮件,等等。当用户没有在 [Birthday] 单元格中输入任何内容并且它的 DateTime (顺便说一句是 Nullable)值是 Null 时,就会出现问题。
这是我添加参数的方法:
private void AddParam(string Key, object Value)
{
if (Value == null)
Value = string.Empty;
ParamStringBuilder.Append(string.Format("{0}={1}&", Key, HttpUtility.UrlEncode(Value.ToString())));
}
以下是我发送 WebRequest 的方式:
string _URL = URIBuilder(Action);
HttpWebRequest httpWebRequest = HttpWebRequest.Create(_URL) as HttpWebRequest;
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Headers.Add("X-Forwarded-For", HttpContext.Current.Request.UserHostAddress);// this is optional to log the original client ip address
// add culture info of calling client
if (SendBrowserCulture)
{
if (HttpContext.Current.Request.Headers["Accept-Language"] != null) // this is optional to access the api with the original culture settings
httpWebRequest.Headers.Add("Accept-Language", HttpContext.Current.Request.Headers["Accept-Language"].ToString());
}
using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream()))
{
sw.Write(ParamStringBuilder);
}
// reset paramstringbuilder
ParamStringBuilder.Clear();
//Getting the Respose and reading the result.
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
APIResponse _Response = new APIResponse(httpWebResponse.GetResponseStream());
return _Response;
我怀疑当 SQL 获取 URLencoded 命令“Birthday=null”时,它会将其读取为
INSERT 'null' INTO Table.Birthday
这当然没有任何意义,所以它用'0001-01-01'填充它
我希望我足够清楚。很高兴再解释一些。
Thx提前寻求帮助!