import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; /** \file * * Oct 15, 2004 * * Copyright Ian Kaplan 2004, Bear Products International * * You may use this code for any purpose, without restriction, * including in proprietary code for which you charge a fee. * In using this code you acknowledge that you understand its * function completely and accept all risk in its use. * * @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ /** * SynchronousHttp * Oct 15, 2004 * * @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ /** Support synchronous HTTP transactions to a given URL. The URL, in String form, is passed in the constructor. */ public class SynchronousHttp { private String mURLString; private URL mURLObj = null; private int mMsgStatus; private String mStatusMsg; /** The constructor is initialized with the URL for this connection. The URL should include the "http://" prefix. */ public SynchronousHttp( String url ) { mURLString = url; } /** */ private HttpURLConnection initHttpConnection( URL urlObj ) { HttpURLConnection returnConn = null; try { HttpURLConnection httpConn = (HttpURLConnection)urlObj.openConnection(); httpConn.setRequestMethod("POST"); httpConn.setDoInput( true ); httpConn.setDoOutput( true ); httpConn.setUseCaches( false ); httpConn.setAllowUserInteraction( false ); httpConn.setRequestProperty("Context-Type", "text/html"); httpConn.connect(); returnConn = httpConn; } catch (IOException e ) { System.out.println("initHttpConnection: " + e ); } return returnConn; } // initHttpConnection /** Support for synchronous HTTP transactions. That is, a message is sent over an HTTP connection to the URL described by the urlObj and the function waits for a reply. */ public char[] synchronousHttp( String sendString ) { char[] responseMsg = null; if (mURLObj == null) { try { mURLObj = new URL( mURLString ); } catch (MalformedURLException e) { System.out.println("synchronousHttp: MalformedURLException = " + e ); mURLObj = null; } } if (mURLObj != null) { HttpURLConnection httpConn = initHttpConnection( mURLObj ); try { DataOutputStream dos = new DataOutputStream( httpConn.getOutputStream() ); dos.writeBytes( sendString ); mMsgStatus = httpConn.getResponseCode(); mStatusMsg = httpConn.getResponseMessage(); } catch (IOException e ) { System.out.println("synchronousHttp: IOException (1) = " + e ); } int cl = httpConn.getContentLength(); if (cl > 0) { try { InputStreamReader istream = new InputStreamReader(httpConn.getInputStream()); BufferedReader httpReader = new BufferedReader( istream ); responseMsg = new char[ cl ]; httpReader.read( responseMsg ); } catch (IOException e ) { System.out.println("synchronousHttp: IOException (1) = " + e ); } } } return responseMsg; } // SynchronousHttp public int getStatus() { return mMsgStatus; } public String getStatusMessage() { return mStatusMsg; }; }