この章では、Web Services Client for C++ が提供する Cookie のサポートについて説明します。サービスからの Cookieの取得、ほかのサービスへの Cookie の追加、スタブ・インスタンスからの Cookie の除去などを説明します。
次の表に、Web Services Client for C++ による Cookie 属性の取り扱いをまとめます。
属性 | 振る舞い |
---|---|
expires | この属性は無視されます。サーバーがクライアントに対して、Cookie を有効期限切れにするよう要求するシグナルを送信しても、クライアントはこれを実行しません。サーバーによりこの属性が設定された後、クライアントはそのスタブを使用して、引き続き各要求時に Cookie を送信します。新しいスタブ・インスタンスが作成され、使用されると、新しいスタブ・インスタンスからの要求時に、元のスタブ・インスタンスから Cookie は送信されません。 |
path | この属性は無視されます。パスに適用できる URI に対する要求だけではなく、すべての要求時に Cookie が送信されます。 |
domain | この属性は無視されます。Cookie はスタブとのアフィニティーを持ち、ドメイン中立型です。 |
secure | この属性は無視されます。Cookie に secure を設定しても、この属性の効果はなく、チャネルがセキュアであるかどうかに関係なく、それ以降のすべての要求時に Cookie が送信されます。 |
1 つのサービスでログインを行い、別のサービスでログイン・セッション Cookie が必要な場合など、スタブの異なるインスタンスで Cookie を必要とする場合、以下に示す例の API を使用することができます。この例では、電卓サービスとログイン・サービスの 2 つのインスタンスを使用しています。1 つ目のインスタンスは、ログイン・サービスを使用して、セッション Cookie を表すいくつかの Cookie を受け取ります。これらの Cookie は、電卓サービスをホストするサーバーに対する認証のための、電卓サービスとの対話に必要です。
Call the webservice LoginService loginService("http://loginserver/loginservice"); // must tell the service to save cookies loginService.setMaintainSession(true); // login so that we can get the session cookies back loginService.login("myusername", "mypassword"); // Store the cookies so they can be given to the Calculator web service as // authentication. int currentCookieKey=0; string cookieKeys[2]; const char* key = loginService.getFirstTransportPropertyKey(); string keyString(key); if(key) { // Only get the "Set-Cookie" transport properties - as these are what the server // sends to the client to set cookies. if(keyString.compare("Set-Cookie")==0) { string valueString(loginService.getCurrentTransportPropertyValue()); cookieKeys[currentCookieKey++] = valueString; } } // then the rest of the cookies while(key = loginService.getNextTransportPropertyKey()) { string nextKeyString(key); // Only get the "Set-Cookie" transport properties - as these are what the server // sends to the client to set cookies. if(nextKeyString.compare("Set-Cookie")==0) { string valueString(loginService.getCurrentTransportPropertyValue()); cookieKeys[currentCookieKey++] = valueString; } } // Now we've logged in and stored the cookies we can create the calculator service, // set the cookies on that stub instance and use the calculator. Calculator calculator("http://calculatorserver/calculatorservice); calculator.setMaintainSession(true); // OK, Now add the previously saved session cookies on to this new service // as this service does not pick up the cookies from the other stub. currentCookieKey=0; while(currentCookieKey< 3) { calculator.setTransportProperty("Cookie", cookieKeys[currentCookieKey++].c_str()); } // Now, when we use the service it will send the session cookies to the server // in the http message header // which allows the server to authenticate this instance of the service. int result = calculator.add(1,2); // If we continue to use this instance of the calculator stub then the cookies // will be continue to be sent. result = calculator.add(1,2); // If we use a new instance of the calculator then it will fail because we have // not set the cookies Calculator newCalculatorInstance("http://calculatorserver/calculatorservice); // This will fail with an exception because we have not set the authentication // cookies result = newCalculatorInstance.add(1,2);
スタブ・インスタンスからの Cookie の除去が必要になることがあります。
service.deleteTransportProperty(cookiename);
calculator.deleteTransportProperty("loginCookie");
service.deleteTransportProperty("Cookie");
「Cookie」の「C」は大文字であることに注意してください。
calculator.deleteTransportProperty("Cookie");