| リクエスト |
GET /WebSite1/WebService.asmx/getHello?str=string HTTP/1.1 Host: localhost |
| レスポンス |
HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://tempuri.org/">string</string> |
| リクエスト |
POST /WebSite1/WebService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/getHello"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getHello xmlns="http://tempuri.org/">
<str>string</str>
</getHello>
</soap:Body>
</soap:Envelope>
|
| レスポンス |
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getHelloResponse xmlns="http://tempuri.org/">
<getHelloResult>string</getHelloResult>
</getHelloResponse>
</soap:Body>
</soap:Envelope>
|
<?xml version="1.0" encoding="utf-8"?>
<s:schema xmlns:s="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="getHello">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="str" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getHelloResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getHelloResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="string" nillable="true" type="s:string" />
</s:schema>
|
そのため、パラメータの数が多くなってくると、URLが非常に長くなる危険がある。「RFC2616 Hypertext Transfer Protocol -- HTTP/1.1」によれば、HTTPの仕様上はURLに長さ制限はないが、ネットワーク環境によっては問題が起きる可能性があるので、長くなり過ぎないように注意しなければならない。
例. http://hostname/hoge.cgi?id=1234 http://hostname/hoge.cgi/id/1234/ 改行、スペース、日本語などはエンコード
日本語を扱う場合は、文字コードの扱いや、特殊文字の文字化けに注意しなければならない。「一貫してUTF-8で通す」とするのが確実である。
サーバ側 入力 Java:request.getParameter("param1")
PHP:$_GET['param1']
その他:QUERY_STRINGなど出力 HTMLではなくXMLを返す クライアント側 リクエスト HTTPかTCPを操作するメソッドを利用
JavaScript:XMLHttpRequest、ActiveXObject ("Microsoft.XMLHTTP")
Java:HttpURLConnectionレスポンス XMLが返ってくるので、DOM APIで操作
・RESTアクセスのサンプル(JavaScript)URLの最後に、現在のミリ秒を追加しているのは、途中の経路でキャッシュを参照されるのを防ぐためである。
function requestRest(){ var endpoint = "http://ホスト名/WebSite1/WebService.asmx"; // 送信先 var param1 = "keywork"; // パラメータ // URLを生成 var url = endpoint + "/getHello?str=" + encodeURI(param1) + "&dummy="+(new Date().getTime()); // XMLHttpRequestを作成 try { xmlhttp = new XMLHttpRequest(); // Netscape, Firefoxなど } catch (e){ try { xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP"); // IE } catch (e){ try { xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP"); // IE } catch (e){ // XMLHttpRequestの作成に失敗 } } } xmlhttp.onreadystatechange = method1; // レスポンスを受け取った時に呼ばれるメソッドを指定 xmlhttp.open('GET', url, true); xmlhttp.setRequestHeader ("Content-Type","text/xml; charset=utf-8"); xmlhttp.send(); // RESTで送信 } function method1() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { alert(xmlhttp.responseText); // 結果をデバッグ表示 } }
・SOAPアクセスのサンプル(JavaScript)しかし、これではパラメータの型はチェックされないし、 "<"のような文字が入っていてもそのまま送信してしまう。DOMのAPIを使っていないために、送信前に「スキーマに合致しているか?」や「XMLとして正しいか?」をチェックすることができない。ミドルウエアで自動生成されたコードを使っていれば、コンパイル時に問題を検出できたり、インラインヘルプといった開発ツールの支援機能を有効に使うことができる。
function requestSoap(){ var endpoint = "http://ホスト名/WebSite1/WebService.asmx"; // 送信先 var param1 = "keywork"; // パラメータ // SOAPメッセージを作成 var msg = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\ <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n\ xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n\ xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n\ <soap:Body>\n\ <getHello xmlns=\"http://tempuri.org/\">\n\ <str>" + param1 + "</str>\n\ </getHello>\n\ </soap:Body>\n\ </soap:Envelope>"; // XMLHttpRequestを作成 try { xmlhttp = new XMLHttpRequest(); // Netscape, Firefoxなど } catch (e){ try { xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP"); // IE } catch (e){ try { xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP"); // IE } catch (e){ // XMLHttpRequestの作成に失敗 } } } xmlhttp.onreadystatechange = method2; // レスポンスを受け取った時に呼ばれるメソッドを指定 xmlhttp.open('POST', endpoint, true); xmlhttp.setRequestHeader ("Content-Type","text/xml; charset=utf-8"); xmlhttp.setRequestHeader ("SOAPAction","\"http://tempuri.org/getHello\""); xmlhttp.send(msg); // SOAPメッセージを送信 } function method2() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { xmlDoc = xmlhttp.responseXML; // 結果をDOMオブジェクトとして取得 } }
・各方式の比較
SOAP REST JSON 学習容易性 × ○ ○ コーディング量(自動生成分は除く) ○ × ○ テストのし易さ × ◎ × 開発ツール ○ × × 導入の容易性 × ○ ○ 開発にかかるトータル時間 × ○ ◎ 複雑なデータの入力 ○ × × 複雑なデータの出力 ○ ○ △ 厳密な型チェック ○ × × 汎用性(扱える開発言語の種類) ○ ○ × 標準化団体 W3C - ecma 拡張性(WS-*) ◎ × ×
Copyright (c) XML コンソーシアム 2007 All rights reserved. Copyright (c) アドソル日進株式会社 2007 All rights reserved.