AppFxExample.java

package appfxexample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class AppFxExample {

    public static void main(String[] args) 
            throws MalformedURLException, IOException {
        //Declare a String for the URL endpoint for the AppFxWebService.
        //The value is hard-coded for this example.
        String urlString = 
"http://localhost/bbAppFx/appfxwebservice.asmx";

        //Declare an HttpURLConnection object for the connection.
        //This requires a new URL object based on the URL String, a 
        //URLConnection object, and an HttpURLConnection object instantiated
        //through a casting of the URLConnection object to HttpURLConnection
        URL urlForInfWebSvc = new URL(urlString);
        URLConnection UrlConnInfWebSvc = urlForInfWebSvc.openConnection();
        HttpURLConnection httpUrlConnInfWebSvc = 
                (HttpURLConnection) UrlConnInfWebSvc;

        //Establish some properites for the HttpURLConnection object.
        httpUrlConnInfWebSvc.setDoOutput(true);
        httpUrlConnInfWebSvc.setDoInput(true);
        httpUrlConnInfWebSvc.setAllowUserInteraction(true);
        
        //Establish properties for the connection related to the requests it 
        //sends. The SOAP message is to be contained in an HTTP POST request.
        //Notice that Content-Length is not set. The connection will handle 
        //that. For SOAP 1.1, you would have to set SOAPAction as well. This 
        //example uses SOAP 1.2.

/*
Example header from the generated Endpoint Reference: 

SOAP 1.2

POST /bbAppFx/appfxwebservice.asmx HTTP/1.1
Host: localhost
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length     
*/
        httpUrlConnInfWebSvc.setRequestMethod("POST");
        httpUrlConnInfWebSvc.setRequestProperty("Host", "localhost");
        httpUrlConnInfWebSvc.setRequestProperty
                ("Content-Type","application/soap+xml; charset=utf-8");

//Declare an OutputStreamWriter object to write the request through the 
//connection.
        OutputStreamWriter infWebSvcReqWriter = 
                new OutputStreamWriter(httpUrlConnInfWebSvc.getOutputStream());

//Declare a string to hold the SOAP message. The message is hard-coded in this
//example.
        String infWebSvcRequestMessage = 
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
             "  xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
             "  xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" +
"  <soap12:Body>" +
"    <CodeTableGetDescriptionRequest xmlns=\"Blackbaud.AppFx.WebService.API.1\">" +
"      <ClientAppInfo REDatabaseToUse=\"BBInfinity\" ClientAppName=\"WebShell\" TimeOutSeconds=\"0\" RunAsUserID=\"00000000-0000-0000-0000-000000000000\" ClientUICulture=\"en-us\" ClientCulture=\"en-us\" />"  +              
"      <CodeTableName>ADDRESSTYPECODE</CodeTableName>" +
"    </CodeTableGetDescriptionRequest>" +
"  </soap12:Body>" +
"</soap12:Envelope>";

//Write the request message and flush the writer.
        infWebSvcReqWriter.write(infWebSvcRequestMessage);
        infWebSvcReqWriter.flush();
        
//Create a reader for the reply and read the rely line by line. Use a String to
//hold a copy of each line and use the String for the exit condition of a while
//loop that adds the value of each line to a String that holds the entire reply.
        BufferedReader infWebSvcReplyReader = 
                new BufferedReader
                        (new InputStreamReader
                                (httpUrlConnInfWebSvc.getInputStream()));
        String line;
        String infWebSvcReplyString = "";
        while ((line = infWebSvcReplyReader.readLine()) != null) {
            infWebSvcReplyString = infWebSvcReplyString.concat(line);
            }
        
//Close the writer and reader. Disconnect the HttpURLConnection.
        infWebSvcReqWriter.close();
        infWebSvcReplyReader.close();
        httpUrlConnInfWebSvc.disconnect();
        
//Print the reply message to the output buffer.        
        System.out.println(infWebSvcReplyString);
    }
}