Call web service from Java

import java.net.*;
import java.io.*;

public class EDCJavaWSInterface {

    public static double GetTicketBalance(String aimsServer, String ticketId) throws IOException
    {
        // initialize the connection to the web service
        URL url = new URL(aimsServer + "/Tickets.asmx");
   
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection)connection;
       
        // build the xml request
        String xmlInput = "<?xml version="1.0" encoding="utf-8"?> " +
        " <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" " +
        "    xmlns:xsd="http://www.w3.org/2001/XMLSchema" " +
        "    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> " +
        "    <soap:Body> " +
        "       <GetTicketBalance xmlns="http://www.edc-aim.com/schemas/tickets_ws/"> " +
        "          <ticketid>" + ticketId + "</ticketid> " +
        "       </GetTicketBalance> " +
        "    </soap:Body> " +
        " </soap:Envelope>";
       
        byte[] buffer = new byte[xmlInput.length()];
        buffer = xmlInput.getBytes();
       
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        bout.write(buffer);
        byte[] b = bout.toByteArray();
       
        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("SOAPAction", "http://www.edc-aim.com/schemas/tickets_ws/GetTicketBalance");
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
       
        // send our request to the web service
        OutputStream out = httpConn.getOutputStream();
        out.write(b);
        out.close();
     
        // read the response from the web service
        InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
        BufferedReader in = new BufferedReader(isr);
     
        String sPart = "";
        String sXML = "";
        while ((sPart = in.readLine()) != null) {
            sXML = sXML + sPart;
        }
       
        // parse the xml response: get the value
        String s = SimpleXMLValueGetter(sXML, "<GetTicketBalanceResult>", "</GetTicketBalanceResult>");
       
        //convert to the proper type
        if (IsNullOrEmpty(s))
        {
            return 0;
        }
        else
        {
            return Double.parseDouble(s);
        }
    }
   
    public static double GetPermitTypeAmount(String aimsServer, String ptypeId) throws IOException
    {
        // initialize the connection to the web service
        URL url = new URL(aimsServer + "/Permits.asmx");
   
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection)connection;
       
        // build the xml request
        String xmlInput = "<?xml version="1.0" encoding="utf-8"?> " +
        " <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" " +
        "    xmlns:xsd="http://www.w3.org/2001/XMLSchema" " +
        "    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> " +
        "    <soap:Body> " +
        "       <CalculatePermitTypeAmount xmlns="http://www.edc-aim.com/schemas/permits_ws/"> " +
        "          <ptypeid>" + ptypeId + "</ptypeid> " +
        "          <startdate xsi:nil="true" /> " +
        "          <enddate xsi:nil="true" /> " +
        "       </CalculatePermitTypeAmount> " +
        "    </soap:Body> " +
        " </soap:Envelope>";
       
        byte[] buffer = new byte[xmlInput.length()];
        buffer = xmlInput.getBytes();
       
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        bout.write(buffer);
        byte[] b = bout.toByteArray();
       
        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("SOAPAction", "http://www.edc-aim.com/schemas/permits_ws/CalculatePermitTypeAmount");
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
       
        // send our request to the web service
        OutputStream out = httpConn.getOutputStream();
        out.write(b);
        out.close();
     
        // read the response from the web service
        InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
        BufferedReader in = new BufferedReader(isr);
     
        String sPart = "";
        String sXML = "";
        while ((sPart = in.readLine()) != null) {
            sXML = sXML + sPart;
        }
       
        // parse the xml response: get the value
        String s = SimpleXMLValueGetter(sXML, "<CalculatePermitTypeAmountResult>", "</CalculatePermitTypeAmountResult>");
       
        //convert to the proper type
        if (IsNullOrEmpty(s))
        {
            return 0;
        }
        else
        {
            return Double.parseDouble(s);
        }
    }
   
    public static String RenewPermit(String aimsServer, String permitNumber, String permitTypeCode,
            String expirationDate, String paymentTypeCode, String permitAmount,
            String tax1, String tax2, String transactionDate, String orderId,
            String transItemId, String paidViaCode) throws IOException
    {
        // initialize the connection to the web service
        URL url = new URL(aimsServer + "/Permits.asmx");
   
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection)connection;
       
        // build the xml request
        String xmlInput = "<?xml version="1.0" encoding="utf-8"?> " +
        " <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" " +
        "    xmlns:xsd="http://www.w3.org/2001/XMLSchema" " +
        "    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> " +
        "    <soap:Body> " +
        "       <Kingston_Renew_Permit xmlns="http://www.edc-aim.com/schemas/permits_ws/"> " +
        "          <Permit_Number>" + permitNumber + "</Permit_Number> " +
        "          <Permit_Desc>" + permitTypeCode + "</Permit_Desc> " +
        "          <Permit_Exp_Date>" + expirationDate + "</Permit_Exp_Date> " +
        "          <Payment_Type>" + paymentTypeCode + "</Payment_Type> " +
        "          <Payment_Amount_Base>" + permitAmount + "</Payment_Amount_Base> " +
        "          <Payment_Amount_Tax1>" + tax1 + "</Payment_Amount_Tax1> " +
        "          <Payment_Amount_Tax2>" + tax2 + "</Payment_Amount_Tax2> " +
        "          <Transaction_Date>" + transactionDate + "</Transaction_Date> " +
        "          <Order_Id>" + orderId + "</Order_Id> " +
        "          <TransItem_Id>" + transItemId + "</TransItem_Id> " +
        "          <Paid_Via>" + paidViaCode + "</Paid_Via> " +        
        "          <userid xsi:nil="true" /> " +
        "          <termid xsi:nil="true" /> " +
        "       </Kingston_Renew_Permit> " +
        "    </soap:Body> " +
        " </soap:Envelope>";
       
        byte[] buffer = new byte[xmlInput.length()];
        buffer = xmlInput.getBytes();
       
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        bout.write(buffer);
        byte[] b = bout.toByteArray();
       
        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("SOAPAction", "http://www.edc-aim.com/schemas/permits_ws/Kingston_Renew_Permit");
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
       
        // send our request to the web service
        OutputStream out = httpConn.getOutputStream();
        out.write(b);
        out.close();
     
        // read the response from the web service
        InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
        BufferedReader in = new BufferedReader(isr);
     
        String sPart = "";
        String sXML = "";
        while ((sPart = in.readLine()) != null) {
            sXML = sXML + sPart;
        }
       
        // parse the xml response: get the value
        String s = SimpleXMLValueGetter(sXML, "<Kingston_Renew_PermitResult>", "</Kingston_Renew_PermitResult>");
       
        // no type conversion necessary
        return s;
    }
   
    public static String RenewPermitImport(String aimsServer, String permitNumber, String permitTypeCode,
            String expirationDate, String paymentTypeCode, String permitAmount,
            String tax1, String tax2, String transactionDate, String orderId,
            String userId, String terminalId) throws IOException
    {
        // initialize the connection to the web service
        URL url = new URL(aimsServer + "/Permits.asmx");
   
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection)connection;
       
        // build the xml request
        String xmlInput = "<?xml version="1.0" encoding="utf-8"?> " +
        " <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" " +
        "    xmlns:xsd="http://www.w3.org/2001/XMLSchema" " +
        "    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> " +
        "    <soap:Body> " +
        "       <Kingston_Renew_Permit xmlns="http://www.edc-aim.com/schemas/permits_ws/"> " +
        "          <Permit_Number>" + permitNumber + "</Permit_Number> " +
        "          <Permit_Desc>" + permitTypeCode + "</Permit_Desc> " +
        "          <Permit_Exp_Date>" + expirationDate + "</Permit_Exp_Date> " +
        "          <Payment_Type>" + paymentTypeCode + "</Payment_Type> " +
        "          <Payment_Amount_Base>" + permitAmount + "</Payment_Amount_Base> " +
        "          <Payment_Amount_Tax1>" + tax1 + "</Payment_Amount_Tax1> " +
        "          <Payment_Amount_Tax2>" + tax2 + "</Payment_Amount_Tax2> " +
        "          <Transaction_Date>" + transactionDate + "</Transaction_Date> " +
        "          <Order_Id>" + orderId + "</Order_Id> " +
        "          <TransItem_Id xsi:nil="true" /> " +
        "          <Paid_Via>POS</Paid_Via> " +        
        "          <userid> " + userId + "</userid> " +
        "          <termid> " + terminalId + "</termid> " +
        "       </Kingston_Renew_Permit> " +
        "    </soap:Body> " +
        " </soap:Envelope>";
       
        byte[] buffer = new byte[xmlInput.length()];
        buffer = xmlInput.getBytes();
       
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        bout.write(buffer);
        byte[] b = bout.toByteArray();
       
        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("SOAPAction", "http://www.edc-aim.com/schemas/permits_ws/Kingston_Renew_Permit");
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
       
        // send our request to the web service
        OutputStream out = httpConn.getOutputStream();
        out.write(b);
        out.close();
     
        // read the response from the web service
        InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
        BufferedReader in = new BufferedReader(isr);
     
        String sPart = "";
        String sXML = "";
        while ((sPart = in.readLine()) != null) {
            sXML = sXML + sPart;
        }
       
        // parse the xml response: get the value
        String s = SimpleXMLValueGetter(sXML, "<Kingston_Renew_PermitResult>", "</Kingston_Renew_PermitResult>");
       
        // no type conversion necessary
        return s;
    }
   
    public static String SaveTicketPayment(String aimsServer, String paymentTypeCode,
            String amount, String transItemId, String postingDate, String orderId,
            String ticketNumber) throws IOException
    {
        // initialize the connection to the web service
        URL url = new URL(aimsServer + "/Tickets.asmx");
   
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection)connection;
       
        // build the xml request
        String xmlInput = "<?xml version="1.0" encoding="utf-8"?> " +
        "      <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> " +
        "        <soap:Body> " +
        "          <MakeTicketPayment xmlns="http://www.edc-aim.com/schemas/tickets_ws/"> " +
        "            <payment> " +
        "              <PaymentTypeCode>" + paymentTypeCode + "</PaymentTypeCode> " +
        "              <Amount>" + amount + "</Amount> " +
        "              <CrcdNumber /> " +
        "              <CrcdAuth>" + transItemId + "</CrcdAuth> " +
        "              <CrcdExp /> " +
        "              <PaidViaCode>GBIZ</PaidViaCode> " +
        "              <CheckNum /> " +
        "              <PostingDate>" + postingDate + "</PostingDate> " +
        "              <ReceiptNumber>" + orderId + "</ReceiptNumber> " +
        "            </payment> " +
        "            <ticket_number>" + ticketNumber + "</ticket_number> " +
        "          </MakeTicketPayment> " +
        "        </soap:Body> " +
        "      </soap:Envelope>";
       
        byte[] buffer = new byte[xmlInput.length()];
        buffer = xmlInput.getBytes();
       
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        bout.write(buffer);
        byte[] b = bout.toByteArray();
       
        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("SOAPAction", "http://www.edc-aim.com/schemas/tickets_ws/MakeTicketPayment");
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
       
        // send our request to the web service
        OutputStream out = httpConn.getOutputStream();
        out.write(b);
        out.close();
       
        // read the response from the web service
        InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
        BufferedReader in = new BufferedReader(isr);
     
        String sPart = "";
        String sXML = "";
        while ((sPart = in.readLine()) != null) {
            sXML = sXML + sPart;
        }
       
        // parse the xml response: get the value
        String s = SimpleXMLValueGetter(sXML, "<MakeTicketPaymentResult>", "</MakeTicketPaymentResult>");
       
        return s;
    }
   
    public static String SaveTicketPaymentImport(String aimsServer, String paymentTypeCode,
            String amount, String postingDate,  String transItemId, String userId, String terminalId,
            String ticketNumber) throws IOException
    {
        // initialize the connection to the web service
        URL url = new URL(aimsServer + "/Tickets.asmx");
   
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection)connection;
       
        // build the xml request
        String xmlInput = "<?xml version="1.0" encoding="utf-8"?> " +
        " <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" " +
        "    xmlns:xsd="http://www.w3.org/2001/XMLSchema" " +
        "    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> " +
        "    <soap:Body> " +
        "       <MakeTicketPaymentUserTerm xmlns="http://www.edc-aim.com/schemas/tickets_ws/"> " +
        "            <payment> " +
        "              <PaymentTypeCode>" + paymentTypeCode  + "</PaymentTypeCode> " +
        "              <Amount>" + amount  + "</Amount> " +
        "              <CrcdNumber /> " +
        "              <CrcdAuth />" +
        "              <CrcdExp /> " +
        "              <PaidViaCode>POS</PaidViaCode> " +
        "              <CheckNum /> " +
        "              <PostingDate>" + postingDate  + "</PostingDate> " +
        "              <ReceiptNumber>" + transItemId  + "</ReceiptNumber> " +
        "            </payment> " +
        "            <ticket_number>" + ticketNumber  + "</ticket_number> " +
        "            <user_name>" + userId  + "</user_name> " +
        "            <term_name>" + terminalId  + "</term_name> " +
        "       </MakeTicketPaymentUserTerm> " +
        "    </soap:Body> " +
        " </soap:Envelope>";
       
        byte[] buffer = new byte[xmlInput.length()];
        buffer = xmlInput.getBytes();
       
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        bout.write(buffer);
        byte[] b = bout.toByteArray();
       
        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("SOAPAction", "http://www.edc-aim.com/schemas/tickets_ws/MakeTicketPaymentUserTerm");
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
       
        // send our request to the web service
        OutputStream out = httpConn.getOutputStream();
        out.write(b);
        out.close();
     
        // read the response from the web service
        InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
        BufferedReader in = new BufferedReader(isr);
     
        String sPart = "";
        String sXML = "";
        while ((sPart = in.readLine()) != null) {
            sXML = sXML + sPart;
        }
       
        // parse the xml response: get the value
        String s = SimpleXMLValueGetter(sXML, "<MakeTicketPaymentUserTermResult>", "</MakeTicketPaymentUserTermResult>");
       
        return s;
    }
   
    public static String SimpleXMLValueGetter(String xml, String startkey, String endkey)
    {
        if (IsNullOrEmpty(xml) || IsNullOrEmpty(startkey) || IsNullOrEmpty(endkey))
        {
            // invalid inputs
            return "";
        }
       
        int iStartKeyLength = startkey.length();
        int iStartKeyPos = xml.indexOf(startkey);
        int iEndKeyPos = xml.indexOf(endkey);
       
        if ((iStartKeyPos < 0) || (iEndKeyPos < 0))
        {
            // could not find key in string
            return "";
        }
       
        return xml.substring(iStartKeyPos + iStartKeyLength, iEndKeyPos);
    }
   
    public static boolean IsNullOrEmpty(String s)
    {
        if(s == null || s.length() == 0)
        {
            return true;
        }  
        else
        {
            return false;
        }
    }
}

Leave a Reply