import java.net.URLConnection; import java.net.URL; import java.net.URLEncoder; import java.net.MalformedURLException; import java.net.HttpURLConnection; import java.io.UnsupportedEncodingException; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.BufferedInputStream; import java.io.FileNotFoundException; import java.util.Properties; import java.util.Enumeration; import javax.swing.JOptionPane; public class pmConnectionManager { //Create an URLConnection object public static URLConnection getConnection (String strLocation, boolean allowUpload, boolean allowDownload) throws Exception { URL urlConnection = null; URLConnection connection = null; try { urlConnection = new URL(strLocation); } catch (MalformedURLException ex) { throw new Exception("Could not connect to the webpage\nThis could be because you are not connected to the Internet"); } try { connection = urlConnection.openConnection(); connection.setDoOutput(allowUpload); connection.setDoInput(allowDownload); } catch (IOException ex) { throw new Exception("Could not open the connection\n" + ex.getMessage()); } return connection; } //Text data does not require all the content-type stuff, so can use a simple printWriter public static Properties sendAndRecTextData (URLConnection connection, Properties props) throws IOException { Enumeration enuData = props.keys(); String name = ""; String value = ""; String strEnd = ""; PrintWriter out = new PrintWriter(connection.getOutputStream()); while (enuData.hasMoreElements()) { name = (String)enuData.nextElement(); value = props.getProperty(name); if (enuData.hasMoreElements()) { strEnd = "&"; } else { strEnd = ""; } try { out.print(name + "=" + URLEncoder.encode(value, "UTF-8") + strEnd); } catch (UnsupportedEncodingException ex) //Will never happen { throw new IOException("Encode type not reconised"); } } out.close(); return recieveData(connection); } //Data (such as images) require a strict header, so uses a different method public static Properties sendAndRecMultiData (URLConnection connection, Properties props, scriptUploadData uploadData) throws IOException { //scriptUploadData collects the different files the user wants to upload Enumeration enuData = props.keys(); String name = ""; String value = ""; String strEnd = ""; String strCRLF = "\r\n"; StringBuffer strData = new StringBuffer(); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=AaB03x"); DataOutputStream outStream = new DataOutputStream(connection.getOutputStream()); strData.append("--AaB03x"); strData.append(strCRLF); //Write all the properties while (enuData.hasMoreElements()) { name = (String)enuData.nextElement(); value = props.getProperty(name); strData.append("Content-Disposition: form-data; name=\"" + name + "\""); strData.append(strCRLF + strCRLF + value + strCRLF + "--AaB03x" + strCRLF); } //Write bytes does not send the data, it just stores it in the stream outStream.writeBytes(strData.toString()); for (int i = 0; i < uploadData.size(); i++) { strData = new StringBuffer(); strData.append("Content-Disposition: form-data; name=\"" + uploadData.getRefAt(i) + "\"; filename=\"" + uploadData.getFileNameAt(i) + "\"" + strCRLF); strData.append("Content-Type: " + uploadData.getContentTypeAt(i) + strCRLF); strData.append("Content-Transfer-Encoding: binary" + strCRLF + strCRLF); outStream.writeBytes(strData.toString()); outStream.write(uploadData.getDataAt(i)); strData = new StringBuffer(strCRLF + "--AaB03x--" + strCRLF); outStream.writeBytes(strData.toString()); } //Flush sends all the bytes and empties the stream outStream.flush(); outStream.close(); return recieveData(connection); } private static Properties recieveData (URLConnection connection) throws IOException { BufferedReader in = null; Properties props = new Properties(); try { in = new BufferedReader(new InputStreamReader(connection.getInputStream())); } catch (FileNotFoundException ex) { InputStream err = ((HttpURLConnection)connection).getErrorStream(); if (err == null) { throw ex; } in = new BufferedReader(new InputStreamReader(err)); } String strLine = ""; String[] strKeyValue = null; String strLastKey = ""; while ((strLine = in.readLine()) != null) { strKeyValue = strLine.split("=", 2); if (strLastKey.equals(strKeyValue[0])) { props.setProperty(strKeyValue[0], props.getProperty(strKeyValue[0]) + "\n" + strKeyValue[1]); } else { props.setProperty(strKeyValue[0], strKeyValue[1]); } strLastKey = strKeyValue[0]; } in.close(); return props; } }