zoukankan      html  css  js  c++  java
  • WCF header 域

        [OperationContract]
        [WebInvoke(UriTemplate = "poststr1")]
        public string poststr1(csinfo cs)
        {
            var x = new { x = "poststr1的调用", a = "服务器时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + "  A值:" + cs.a, b = "B值: " + cs.b, c = "C值:" + cs.c };
            return Xtools.json_TtoJson(x);
        }
        [OperationContract]
        [WebInvoke(UriTemplate = "poststr2", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        public string poststr2(string cs)
        {
            var x = new { x = "poststr2的调用", a = "poststr2调用 服务器时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + "  cs值:" + cs };
            return Xtools.json_TtoJson(x);
        }
    

      

    function dy3() { var cs = { a: "dsfdsfds", b: "ssddddd", c: "sdfsfds" }; $.ajax({ type: "POST", url: "http://10.10.12.70/xx1100/poststr1", contentType: "application/json; charset=utf-8", //(可以) // data: '{"a":"axx","b":"bxx","c:":"cxx"}', data: JSON.stringify(cs), dataType: "json", cache: false, async: false, processData: true, success: function (json) { alert(json); var t = eval('(' + json + ')'); alert(t.x); }, error: function (xhr, ajaxOptions, thrownError) { alert("调用出错" + xhr.responseText); } }); };

      

      <system.webServer>
        <httpProtocol>
          <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*"/>
            <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS"/>
            <add name="Access-Control-Allow-Credentials" value="true"/>
            <add name="Access-Control-Allow-Headers" value="Origin, X-Test-Header, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Headers"/>
            <add name="Access-Control-Max-Age" value="3600"/>
          </customHeaders>
        </httpProtocol>
      </system.webServer>
    

      

      public class MessageInspector : IDispatchMessageInspector
        {
    
            public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
            {
    
               
                return request;
            }
    
            public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
            {
    
    
                WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Origin", "*");
                WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Headers", "Origin, X-Test-Header, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Headers");
                //WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
                WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Methods", "GET, POST");
                WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Credentials", "true");
                WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Max-Age", "3600");
                if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS")
                {
          
                }
    
    
                //if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS")
                //{
                //    WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Methods", "GET, POST");
                //    WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Credentials", "true");
                //    WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Headers", "Origin, X-Test-Header, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Headers");
                //    WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Max-Age", "3600");
                //    //WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Methods", "GET, POST");
                //    //HttpContext.Current.Response.End();
                    
                ////WebOperationContext.Current.CreateTextResponse("OK").Close();
                //    //WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NoContent;
                //}
                //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
                //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
                //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true");
                //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Test-Header, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Headers");
                //HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "3600");
                //if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
                //{
                //     HttpContext.Current.Response.End();
                //}
            }
        }
    

      

      public class EbB : IEndpointBehavior  
        {
    
            public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
            {
               // throw new NotImplementedException();
            }
    
            public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
            {
               // throw new NotImplementedException();
    
            }
    
            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
            {
                endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new MessageInspector());
               // throw new NotImplementedException();
            }
    
            public void Validate(ServiceEndpoint endpoint)
            {
               // throw new NotImplementedException();
            }
        }
    

      

    WebHttpBinding wb = new WebHttpBinding();
    
                sh.AddServiceEndpoint(typeof(IService1), wb, "http://localhost/xx1100");
                
    
               // EnableCrossOriginResourceSharingBehavior crossOriginBehavior = new EnableCrossOriginResourceSharingBehavior();
           
    
                foreach (var endpont in sh.Description.Endpoints)
                {
                 endpont.Behaviors.Add(new EbB());
                   // endpont.Behaviors.Add(crossOriginBehavior);
                }
    
                sh.Open();
                button1.Text = "已启动";
    

      

      //[OperationContract]
            //[WebInvoke(UriTemplate = "poststr1",Method="POST",ResponseFormat=WebMessageFormat.Json)]
            //string poststr1(csinfo cs);
            [OperationContract]
            [WebInvoke(UriTemplate = "poststr1",Method="*", ResponseFormat = WebMessageFormat.Json)]
            string poststr1(csinfo cs);
    
            //[OperationContract]
            //[WebInvoke(UriTemplate = "poststr1", Method = "OPTIONS")]
            //void poststr12();
    

      

       //void IService1.poststr12()
            //{
            //    string STR = "DDD";
            //   // throw new NotImplementedException();
            //}
    
            public string poststr1(csinfo cs)
            {
                //if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS")
                //{
                //    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
                //    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "POST");
                //    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept");
                //    WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound();
                //    string ff = "";
                //    return null;
                //}
                //WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*"); 
    
                if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS")
                {
                    //  reply.Close();
                    return "";
                }
    
    
                if (cs == null)
                {
                    return Xtools.json_TtoJson("kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"));
                }
                else
                {
                    //OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("", ""));
                    var x = new { x = "poststr1的调用", a = "服务器时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + "  A值:" + cs.a, b = "B值: " + cs.b, c = "C值:" + cs.c };
                    return Xtools.json_TtoJson(x);
                }
            }
            public string poststr2(string cs)
            {
                var x = new { x = "poststr2的调用", a = "poststr2调用 服务器时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + "  cs值:" + cs };
                return Xtools.json_TtoJson(x);
            }
    
        }
    

      

  • 相关阅读:
    spring mvc配置完后实现下载功能
    表单中Readonly和Disabled的区别(转载)
    EL表达式中fn函数 (转载)
    Spring mvc中@RequestMapping 6个基本用法小结(转载)
    web开发,关于jsp的常见问题,重复提交,防止后退。
    JQuery页面加载
    解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题(转载)
    数据库设计原则(转载)
    dhtmlxTree介绍(转载)
    主键索引
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/6762302.html
Copyright © 2011-2022 走看看