在ASP.NET中接收POST数据
我想在C#桌面应用程序中使用ASP来生成代码.
为此,我设置了一个简单的主机(派生自System.MarshalByRefObject),它通过HttpRuntime.ProcessRequest处理System.Web.Hosting.SimpleWorkerRequest.这将处理传入请求指定的ASPX脚本(使用System.Net.HttpListener等待请求). 客户端部分由System.ComponentModel.BackgroundWorker表示,它构建System.Net.HttpWebRequest并从服务器接收响应. 我的客户端部分代码的简化版本如下所示: private void SendRequest(object sender,DoWorkEventArgs e) { // create request with GET parameter var uri = "http://localhost:9876/test.aspx?getTest=321"; var request = (HttpWebRequest)WebRequest.Create(uri); // append POST parameter request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; var postData = Encoding.Default.GetBytes("postTest=654"); var postDataStream = request.GetRequestStream(); postDataStream.Write(postData,postData.Length); // send request,wait for response and store/print content using (var response = (HttpWebResponse)request.GetResponse()) { using (var reader = new StreamReader(response.GetResponseStream(),Encoding.UTF8)) { _processsedContent = reader.ReadToEnd(); Debug.Print(_processsedContent); } } } 我的服务器部分代码看起来像这样(没有异常处理等): public void ProcessRequests() { // HttpListener at http://localhost:9876/ var listener = SetupListener(); // SimpleHost created by ApplicationHost.CreateApplicationHost var host = SetupHost(); while (_running) { var context = listener.GetContext(); using (var writer = new StreamWriter(context.Response.OutputStream)) { // process ASP script and send response back to client host.ProcessRequest(GetPage(context),GetQuery(context),writer); } context.Response.Close(); } } 到目前为止,只要我使用GET参数,所有这些都可以正常工作.但是当我在ASPX脚本中接收POST数据时,我遇到了麻烦.为了测试我使用以下脚本: // GET parameters are working: var getTest = Request.QueryString["getTest"]; Response.Write("getTest: " + getTest); // prints "getTest: 321" // don't know how to access POST parameters: var postTest1 = Request.Form["postTest"]; // Request.Form is empty?! Response.Write("postTest1: " + postTest1); // so this prints "postTest1: " var postTest2 = Request.Params["postTest"]; // Request.Params is empty?! Response.Write("postTest2: " + postTest2); // so this prints "postTest2: " 我在ASP中处理的System.Web.HttpRequest对象似乎不包含有关我的POST参数“postTest”的任何信息.我在调试模式下检查了它,并且没有成员确实既不包含参数名称“postTest”也不包含参数值“654”.我也尝试过Request的BinaryRead方法,但不幸的是它是空的.这对应于Request.InputStream == null和Request.ContentLength == 0.为了让事情真的让人困惑,Request.HttpMethod成员被设置为“GET”?! 为了隔离问题,我使用PHP脚本而不是ASPX脚本测试了代码.这很简单: print_r($_GET); // prints all GET variables print_r($_POST); // prints all POST variables 结果是: Array ( [getTest] => 321 ) Array ( [postTest] => 654 ) 因此,使用PHP脚本,我可以访问POST数据.为什么ASPX脚本没有?我究竟做错了什么? Response对象中是否有特殊的访问器或方法? 任何人都可以提示或甚至知道如何解决这个问题?提前致谢. 解决方法
这可能是一个编码问题,导致您的请求流被丢弃.你应该使用:
UTF8Encoding.UTF8.GetBytes("postTest=654"); 因为您可能会使用“Windows-1252”代码页发送请求. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – Oracle Managed Dataacess EF6自定义edm映射不适
- asp.net-core – Asp.net核心身份使用AspNetUserClaims或As
- 如何使用ASP.NET获取访问者位置(国家,州和城市)
- asp.net – 我应该加密URL参数传递的GUID吗?
- 使用asp.net标识更新用户角色
- asp.net-mvc-3 – MVC 3 html.TextBoxFor readonly要动态设
- asp.net-mvc-4 – 在区域中使用Url.RouteUrl()和路由名称
- asp.net – 和debug =“false”有什么区别?
- asp.net – 为VS2010和TFS中的团队管理web.config
- asp.net-mvc – 如何在客户端Kendo UI网格中实现服务器端分