HTTPクライアント
HTTPSプロトコルを使用するHTTPクライアントを作成して、Webサービスを呼び出すことができます。
次に示すサンプル・クライアント・コードでは、次のことが実行されます。
-
費用項目および経費精算書サービスでfind操作を呼び出すxmlペイロードを構成します。
-
サービスへの
HTTPUrlConnection
を開きます。 -
要求のコンテンツ・タイプを
xml
に、HTTPメソッドをPOST
に構成します。 -
サービスを呼び出すためのユーザー名とパスワードを指定して、Basic認証を使用するように要求を構成します。
-
xmlペイロードを要求として接続に書き込みます。
-
接続から応答を受け取り、出力を印刷します。
このサンプル・コードを実行するには、次の操作を実行する必要があります。
-
開発者の接続ポータルを使用して、費用項目および経費精算書サービスのホスト名とポートを取得します。
サンプル・コードを使用して別のビジネス・オブジェクト・サービスを呼び出すには、対応するサービス・エンドポイントURLに置き換えます。 たとえば、
HOST
をyour-host-name:your-portに、SVC_RELPATH
を/finExmSharedCommon/ExpenseServiceに置き換えます。 -
クラウド・インスタンスの有効なユーザー名とパスワードを使用して、ユーザー名とパスワード(
"username:password"
)を更新します。 -
keytoolコマンドを使用して、サーバーからクライアントのトラスト・ストアに証明書をインポートします。
public static void http_client() throws Exception {
System.out.println("Invoke service using direct HTTP call with Basic Auth");
String payload =
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
" <soap:Header>\n" +
" <fmw-context xmlns=\"http://xmlns.oracle.com/fmw/context/1.0\"/>\n" +
" </soap:Header>\n" +
" <soap:Body>\n" +
" <ns1:findExpense xmlns:ns1=\"http://xmlns.oracle.com/apps/financials/expenses/shared/common/expenseExternalService/types/\">\n" +
" <ns1:findCriteria xmlns:ns2=\"http://xmlns.oracle.com/adf/svc/types/\">\n" +
" <ns2:fetchStart>0</ns2:fetchStart>\n" +
" <ns2:fetchSize>2</ns2:fetchSize>\n" +
" <ns2:excludeAttribute>true</ns2:excludeAttribute>\n" +
" </ns1:findCriteria>\n" +
" <ns1:findControl xmlns:ns2=\"http://xmlns.oracle.com/adf/svc/types/\">\n" +
" <ns2:retrieveAllTranslations>true</ns2:retrieveAllTranslations>\n" +
" </ns1:findControl>\n" +
" </ns1:findExpense>\n" +
" </soap:Body>\n" +
"</soap:Envelope>";
httpPost("https://" + HOST + SVC_RELPATH + "?invoke=", payload,
"username:password");
}
private static String httpPost(String destUrl, String postData,
String authStr) throws Exception {
URL url = new URL(destUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
if (conn == null) {
return null;
}
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setFollowRedirects(true);
conn.setAllowUserInteraction(false);
conn.setRequestMethod("POST");
byte[] authBytes = authStr.getBytes("UTF-8");
String auth = com.sun.org.apache.xml.internal.security.utils.Base64.encode(authBytes);
conn.setRequestProperty("Authorization", "Basic " + auth);
System.out.println("post data size:" + postData.length());
OutputStream out = conn.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
writer.write(postData);
writer.close();
out.close();
System.out.println("connection status: " + conn.getResponseCode() +
"; connection response: " +
conn.getResponseMessage());
InputStream in = conn.getInputStream();
InputStreamReader iReader = new InputStreamReader(in);
BufferedReader bReader = new BufferedReader(iReader);
String line;
String response = "";
System.out.println("==================Service response: ================ ");
while ((line = bReader.readLine()) != null) {
System.out.println(line);
response += line;
}
iReader.close();
bReader.close();
in.close();
conn.disconnect();
return response;
}