HTTP Client

You can create a HTTP client, which uses the HTTPS protocol, to invoke a web service.

The following is a sample client code to:

  1. Construct an xml payload to invoke the find operation on the Expense Item and Expense Report service.

  2. Open an HTTPUrlConnection to the service.

  3. Configure the request content type to xml and HTTP method to POST.

  4. Configure the request to use basic authentication, with a user name and a password to invoke the service.

  5. Write the xml payload to the connection as request.

  6. Receive the response from the connection, and print the output.

To run this sample code, you must:
  • Get the hostname and port for the Expense Item and Expense Report service using the Developer Connect portal.

    To use the sample code to invoke a different business object service, replace with the corresponding service endpoint URL. For example, replace HOST with your-host-name:your-port and SVC_RELPATH with /finExmSharedCommon/ExpenseService.

  • Update the user name and password ("username:password") with a valid user name and password for your cloud instance.

  • Import the certificate from the server into the client trust store using the keytool command.

 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;
    }
Related Topics
  • Static JAX-WS Proxy Client
  • Dynamic Dispatch Client
  • Java Client
  • Invoking SOAP Web Services