Perl
HTTP::Request
やSOAP::Lite
などの様々な手法を使用してWebサービスを呼び出すには、Perlを使用します。
HTTP::Request
SOAP Webサービス用にXMLを直接作成し、処理するには、HTTP::Requestを使用します。
次に示すサンプル・コードは、次を実行します。
-
サービスを呼び出すxmlペイロードを作成します。 このコード例は、ハードコードされた文字列(UTF-8バイト配列にエンコード)を要求に渡します。
-
サービス・コールの資格証明のため、Base 64でエンコードされた文字列を作成します。
-
サービスのPOST要求を作成します。
-
Base64でエンコードされた資格証明を使用して基本認証を使用するため、要求コンテンツ・タイプを
xml
として構成します。 -
呼び出す
SOAPAction
を設定します。 サービス・コールはこの値がなくても行えますが、これが推奨される標準です。 -
xmlペイロードを要求に書き込みます。
-
サービスを呼び出します。
-
応答を処理します。 この例は、応答を単に出力します。
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use HTTP::Request::Common;
use MIME::Base64;
my $ua = LWP::UserAgent->new(agent => 'perl post');
# Construct xml payload to invoke the service. In the example, it is hard coded.
my $envelope = <<END;
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<findRule
xmlns="http://xmlns.oracle.com/apps/incentiveCompensation/cn/creditSetup/creditRule/creditRuleService/types/">
<findCriteria>
<fetchStart xmlns="http://xmlns.oracle.com/adf/svc/types/">0</fetchStart>
<fetchSize xmlns="http://xmlns.oracle.com/adf/svc/types/">-1</fetchSize>
<filter xmlns="http://xmlns.oracle.com/adf/svc/types/">
<group>
<upperCaseCompare>false</upperCaseCompare>
<item>
<upperCaseCompare>false</upperCaseCompare>
<attribute>RuleId</attribute>
<operator>=</operator>
<value>300000000851162</value>
</item>
</group>
</filter>
<excludeAttribute xmlns="http://xmlns.oracle.com/adf/svc/types/">false</excludeAttribute>
</findCriteria>
<findControl>
<retrieveAllTranslations xmlns="http://xmlns.oracle.com/adf/svc/types/">false</retrieveAllTranslations>
</findControl>
</findRule>
</soap:Body>
</soap:Envelope>
END
# Construct the base 64 encoded string used as the credentials for the service call.
my $credentials = encode_base64('username:password');
my $url = 'https://host:port/icCnSetupCreditRulesPublicService/CreditRuleService';
# Create new POST request
my $req=new HTTP::Request 'POST',$url;
# Configure the request to use basic authentication, with base64-encoded user name and password to invoke the service.
$req->header('Authorization' => "Basic " . $credentials);
# Set the SOAP action to be invoked; while the call works without this, the value is expected to be set based on standards
$req->header('SOAPAction' =>
'http://xmlns.oracle.com/apps/incentiveCompensation/cn/creditSetup/creditRule/creditRuleService/findRule');
# Configure the request content type to be xml, HTTP method to be POST, and set the content length
$req->header('Content-Type' => 'text/xml;charset=UTF-8');
# Write the xml payload to the request.
$req->content($envelope);
# Call the service
my $response=$ua->request($req);
# Process the response. In this example, we simply print the response.
print $response->error_as_HTML unless $response->is_success;
print $response->as_string;
SOAP::Lite
以下は、次を実行するためのSOAP:Liteコード例です。
-
新しいSOAP Liteを作成します。
-
エンベロープの名前空間を登録します。
-
セキュリティ情報を使用してSOAPヘッダーを作成します。
-
サービスを呼び出します。
-
応答を処理します。 この例は、応答を単に出力します。
#!/usr/bin/perl
use lib '/usr/lib/perl5/5.8.8/';
use MIME::Base64;
use SOAP::Lite;
SOAP::Lite->import(trace => debug);
# Create a new instance for the SOAP Lite
my $soap = SOAP::Lite->new(
proxy => 'http://host:port/hcmEmploymentCoreWorker/WorkerService',
service => 'http://host:port/hcmEmploymentCoreWorker/WorkerService?wsdl',
);
# Register namespace(s) for the envelope
my $serializer = $soap->serializer();
$serializer->register_ns( 'http://xmlns.oracle.com/apps/hcm/employment/core/workerService/types/', 'typ' );
# Create SOAP header with security information
my $auth = SOAP::Header->new( name => "wsse:Security" );
$auth->attr( { "xmlns:wsse" => "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecuritysecext-1.0.xsd"});
$auth->mustUnderstand(1);
$auth->value( \SOAP::Data->value(
SOAP::Data->name("wsse:UsernameToken" => \SOAP::Data->value(
SOAP::Data->name("wsse:Username" => 'username')->type(""),
SOAP::Data->name("wsse:Password" => 'password')->type("")
->attr( {"Type" => "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-usernametoken-profile-1.0#PasswordText"}),
SOAP::Data->name("wsse:nonce" => 'jwCzGGijT90Wml6eZe4cxg==')->type("")
->attr( {"EncodingType" => "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsssoap-message-security-1.0#Base64Binary"}),
SOAP::Data->name("wsse:created" => '2012-07-04T06:49:48.981Z')->type("")
)->type("")
)->attr( {"wsu:Id" => "UsernameToken-2",
"xmlns:wsu" => "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-utility-1.0.xsd"
})
)->type("")
);
# Populate parameters to call the service
my $parameters =SOAP::Data->name("typ:personId" => '300000010363707')->type("");
# Call the service
my $result = $soap->call(SOAP::Data->name('typ:getWorker') => $parameters, $auth);
# Process the response. In this example, we simply print the response
die $result->faultstring if ($result->fault);
print $result->result, "\n";