代码之家  ›  专栏  ›  技术社区  ›  DarthVader

web服务与浏览器调用的同步

  •  4
  • DarthVader  · 技术社区  · 16 年前

    我有一个HttpHandler,它在一个请求中查询3个web服务,并将结果存储在一个cookie中。

    正如你所想象的,结果是相互冲突的。以下是如何:

    流程如下: 当我查询服务1并等待结果时,存储结果的cookie还不存在,然后result来自服务2和volia,创建cookie,存储结果,然后response返回服务1并覆盖该cookie,这不应该是这样。

    我应该通过javascript在客户端完成吗?如果是,怎么做?:)

    还是在服务器端?

    所以我不想要异步调用。正确的?

    代码如下:

    if(service1.isEnabled){  
       invokeService1();  
    }
    
    if(service2.isEnabled){  
       invokeService2();  
    }
    
    if(service3.isEnabled){  
       invokeService3();  
    }
    
    invokeService1(){
      callToService1();
      // response comes to another HttpHandler, which is a redirect from the service      
    }
    
    invokeService2(){
      callToService2();  
      // response comes to another HttpHandler, which is a redirect from the service  
    }
    
    invokeService3(){
      callToService3();
      // response comes to another HttpHandler, which is a redirect from the service
    }
    

    然后在HttpHandler上:

    HttpCookie cookie = request.Cookie.Get(MYCookie) ?? new HttpCookie(MYCookie);  
    
    if(request.Params["foo"]){  
      //set cookie content  
    }
    
    if(request.Params["Bar"].isNotNullOrEmpty()){  
    //set cookie content  
    }
    

    我就是这样设置的。我觉得我做饼干的方式是个问题。

    2 回复  |  直到 16 年前
        1
  •  0
  •   feroze    16 年前

    您能序列化请求并让客户机间接地发出它们吗?

    例如:

    客户的第一个请求

    GET /handler.ashx HTTP/1.1
    ....
    

    服务器重新组织请求中没有cookie。所以,它假设这是一个新的连接。具有重定向的服务器响应:

    HTTP/1.1 302 Moved
    Location: http://server.com/handler.ashx?executeService1
    

    客户的第二个请求

    现在客户端向handler.ashx发送一个请求?执行服务1

    处理程序获取此请求并查询WebService1。并在响应中放置cookie(使用另一个重定向)

    HTTP/1.1 302 Moved
    Location: http://server.com/handler.ashx?executeService2
    set-cookie: value
    

    你明白吗?

    其优点是服务器正在与客户端握手。服务器知道需要调用web服务的次数,并可以使用cookie来执行此操作。

    您建议的另一个选项是在客户端使用javascript和AJAX以及XML负载。当webservice调用完成后,您可以使用服务调用的结果调用服务器,服务器可以在客户机上设置cookie。

        2
  •  1
  •   ivo    16 年前

    调用服务 和 呼叫服务 方法,然后在回调上继续执行。

    试试这个:

    var cookieData = {};
    
    var callService = function(service, onCalledService) {
        // TODO #1: contact the server to get the cookie data
        // on server callback:
        //     TODO #2:  append the retrieved data to cookieData
        //     and still inside the server callback do: onCalledService();       
    };
    
    var invokeService = function(service, onInvokedService) {
        if (service.isEnabled) {
            callService(service, function() {
                onInvokedService();
            });
        } else {
            onInvokedService();
        }
    };
    
    invokeService(service1, function () {
    
        invokeService(service2, function () {
    
            invokeService(service3, function () {
                // cookieData now has all the data you need, so:
                // TODO #3: set the cookie content
            });
    
        });
    
    });
    

    这是应该发生的:

    1. invokeService(service1, ...) will check if service1 is enabled
    1.1. if service1 is enabled 
    1.1.1. callService(service1, ...)
    1.1.2. append the retrieved data to cookieData
    1.1.3. call the callback (i.e. go to 2.)
    1.2. if service1 is not enabled 
    1.2.1. call the callback (i.e. go to 2.)
    
    2. invokeService(service2, ...) will check if service2 is enabled
    2.1. if service2 is enabled 
    2.1.1. callService(service2, ...)
    2.1.2. append the retrieved data to cookieData
    2.1.3. call the callback (i.e. go to 3.)
    2.2. if service2 is not enabled 
    2.2.1. call the callback (i.e. go to 3.)
    
    3. invokeService(service3, ...) will check if service3 is enabled
    3.1. if service3 is enabled 
    3.1.1. callService(service3, ...)
    3.1.2. append the retrieved data to cookieData
    3.1.3. call the callback (i.e. go to 4.)
    3.2. if service3 is not enabled 
    3.2.1. call the callback (i.e. go to 4.)
    
    4. set the cookie content