Web 服务和浏览器调用同步

发布于 2024-09-27 04:24:38 字数 1231 浏览 3 评论 0原文

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

正如您所想象的那样,结果会发生冲突。具体方法如下:

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

id 喜欢的是将这些请求排队。

我应该通过 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 时,它会附带查询字符串。

然后在 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  
}

这就是我设置它的方式。我认为我创建cookie的方式是一个问题。

I have a HttpHandler which queries 3 web services within a single request and store the results in a single cookie.

As you would imagine, the results collide. Here is how:

Process is as follows:
when i query service 1 and waiting for the result, the cookie i m storing the results doesnt exist yet, then result comes from service 2 and volia, creates the cookie, stores the result, and then response comes back from service 1 and overwrites that cookie, which shouldnt be the case.

what i d like is to queue these requests.

Should i do it on client side via javascript? if yes, how ?:)

or do it on server side ?

so i dont want asynchronous calls. right?

here is the code:

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
}

when the responses arrive to the HttpHandler, it comes with querystring.

then on that 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  
}

this is how i set it. The way i create the cookie is a problem I think.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷鸟归林 2024-10-04 04:24:38

不,如果您依赖异步调用,一切都会变得更加简单,因此您必须为 invokeServicecallService 方法提供回调函数,然后继续执行回调。

试试这个:

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

No, everything becomes much simpler if you rely on asynchronous calls, so you have to provide callback functions to your invokeService and callService methods, and then continue execution on the callback.

Try this:

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
        });

    });

});

This is what should happen:

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
身边 2024-10-04 04:24:38

您可以序列化请求并让客户端间接发出它们吗?

例如:

来自客户端的第一个请求

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

服务器识别出请求中没有 cookie。因此,它假设这是一个新连接。服务器响应重定向:

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

来自客户端的第二个请求

现在客户端向 handler.ashx?executeService1 发送请求,

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

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

您明白了吗?

优点是服务器端与客户端握手。 SErver 知道必须调用 Web 服务多少次,并且可以使用 cookie 来执行此操作。

您建议的另一个选择是在客户端使用 javascript 和 AJAX 以及 XML 有效负载进行操作。当Web服务调用完成后,您可以使用服务调用的结果来调用服务器,并且服务器可以在客户端上设置cookie。

Can you serialize the requests and make the client issue them indirectly?

for eg:

First request from client

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

Server recorgnizes that there is no cookie in the request. So, it assumes that this is a new connection. Server response with a redirect:

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

Second request from client

Now the client sends a request to handler.ashx?executeService1

THe handler gets this request and queries WebService1. And puts a cookie in the response (with another redirect)

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

You get the idea?

The advantage is that the server is on the handshake with the client. SErver knows how many times the web services have to be called and can use the cookie to do that.

Another option as you suggested is to do on the client side using javascript and AJAX with an XML payload. When the webservice calls are done, you can then call the server with the result of the service calls, and server can set a cookie on the client.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文