JavaApplicationURLConnectionExample.java

package javaapplicationurlconnectionexample;

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 JavaApplicationURLConnectionExample {
     
    public static void main(String[] args) 
            throws MalformedURLException, IOException {
        //Declare a String for the URL endpoint for the Address Type code table 
        //BizOps web service. The value is hard-coded for this example.
        String urlString = 
"http://localhost/bbAppFx/vpp/bizops/db%5BBBINFINITY%5D/codetables/ADDRESSTYPECODE/soap.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 headers from the generated Endpoint Reference: 

SOAP 1.1:
         
POST /bbAppFx/vpp/bizops/db%5BBBINFINITY%5D/codetables/ADDRESSTYPECODE/soap.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "blackbaud_appfx_server_bizops/GetTableEntryList"

SOAP 1.2

POST /bbAppFx/vpp/bizops/db%5BBBINFINITY%5D/codetables/ADDRESSTYPECODE/soap.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>" +
"    <GetTableEntryListRequest xmlns=\"blackbaud_appfx_server_bizops\">" +
"      <IncludeInactive>true</IncludeInactive>" +
"      <RefreshCache>true</RefreshCache>" +
"    </GetTableEntryListRequest>" +
"  </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);
    }
}