asp.net – 通过邮件发送wcf服务消费表单数据
发布时间:2020-12-30 10:39:01 所属栏目:asp.Net 来源:互联网
导读:我读了一些关于这个的文章,我发现要获得wcf从我们添加的帖子请求中获取数据 [ServiceContract]public interface IService1 { [OperationContract] [WebInvoke( Method = POST, BodyStyle = WebMessageBodyStyle.Bare, UriTem
我读了一些关于这个的文章,我发现要获得wcf从我们添加的帖子请求中获取数据 [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke( Method = "POST",BodyStyle = WebMessageBodyStyle.Bare,UriTemplate = "/GetData")] void GetData(Stream data); } 并在实施中 public string GetData( Stream input) { long incomingLength = WebOperationContext.Current.IncomingRequest.ContentLength; string[] result = new string[incomingLength]; int cnter = 0; int arrayVal = -1; do { if (arrayVal != -1) result[cnter++] = Convert.ToChar(arrayVal).ToString(); arrayVal = input.ReadByte(); } while (arrayVal != -1); return incomingLength.ToString(); } 我的问题是我应该怎样做,在表单请求中提交操作会发送到我的服务并消费? 在Stream参数中,我是否可以通过Request [“FirstName”]从表单中获取信息? 解决方法您的代码未正确解码请求正文 – 您正在创建一个字符串值数组,每个字符串值都包含一个字符.获取请求体后,您需要解析查询字符串(使用HttpUtility是一种简单的方法).下面的代码显示了如何正确获取正文和其中一个字段.public class StackOverflow_7228102 { [ServiceContract] public interface ITest { [OperationContract] [WebInvoke( Method = "POST",UriTemplate = "/GetData")] string GetData(Stream data); } public class Service : ITest { public string GetData(Stream input) { string body = new StreamReader(input).ReadToEnd(); NameValueCollection nvc = HttpUtility.ParseQueryString(body); return nvc["FirstName"]; } } 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(); c.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; Console.WriteLine(c.UploadString(baseAddress + "/GetData","FirstName=John&LastName=Doe&Age=33")); Console.Write("Press ENTER to close the host"); Console.ReadLine(); host.Close(); } } (编辑:台州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 删除布局将默认为_ViewStart,为什么找不到部
- 在IIS / ASP.Net中的.NET 1.1应用程序中创建.NET 3.0子应用
- asp.net-mvc – 为什么MVC4捆绑捆绑Knockout.js?
- asp.net – 如何使用resxresourcewriter写入所有三个值?
- 优化 – 字典/客户端VS应用程序变量
- asp.net – 转发器控件中的单选按钮列表
- asp.net – 如何查看Chrome开发者工具中发布到表单的数据大
- asp.net – __doPostBack在DotNetNuke网站上未定义为IE 10
- asp.net-mvc-3 – MVC 3 $.ajax – 响应似乎是从部分视图缓
- asp.net-mvc – asp.net mvc – string或int的路由(即/ typ
推荐文章
站长推荐
- asp.net-web-api2 – 在WebAPI2项目中加载System
- linq – ASP.NET Web API GET方法:为单个参数传
- 模型 – 视图 – 控制器 – ASP.NET WebForms vs
- js触发asp.net的Button的Onclick事件应用
- asp.net – FormsAuthentication.GetRedirectUrl
- asp.net – 在网站上放置广告的最佳做法?
- 如何设置特定于ASP.NET请求的log4net上下文属性?
- asp.net-mvc – SSL安全SaaS应用程序的URL设计
- asp.net-mvc-3 – 如何在ASP.NET MVC中创建递归结
- asp.net-mvc – 为什么DropDownListFor会在提交后
热点阅读