/* ################################################################# # Connect.Java - Insuma GmbH - www.insuma.de - info@insuma.de # # DESCRIPTION # This program resides on the customer side and requests # XML interface of Insuma search eingine. It posts query # XML and receives result set XML in response. # # Watch the comments in the text of the program. # # DISCLAIMER # This program is provided for demonstration # purpose only. Use it at your own risk. While the authors # has made every effort to make this program work correctly, # they cannot accept responsibility for any damages or # losses caused by its usage. # # LEGALESE # THIS INFORMATION AND/OR SOFTWARE IS PROVIDED BY THE AUTHOR(S) # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY # AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. I ABSOLVE # THE AUTHORS, OR INSUMA FROM ANY LIABILITY FOR ANY DIRECT, # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE, DATA, OR PROFITS ; # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY # OF SUCH DAMAGE . # IN NO EVENT SHALL THE PARTIES DESCRIBED ABOVE BE LIABLE FOR # THIRD PARTY INTERFERENCE WITH CONTRACTUAL RELATIONS AS BETWEEN # MYSELF, MY SERVICE PROVIDER, AND/OR INSUMA. # ################################################################ # */ import java.net.*; import java.io.*; // import javax.mail.*; // this is a nicer way of dealing with MIME, // but not available in Standard Edition public class connect { public static void main( String args[] ) throws MalformedURLException, IOException, ProtocolException { URL requestURL = new URL("http://www.insuma.de/cgi-bin/xml/web2xml.py"); String sSearchTerm = "java forever"; HttpURLConnection connection = (HttpURLConnection) requestURL.openConnection(); connection.setDoOutput(true); // causes POST // Set up the HTTP headers // The boundary should not be present in the query. // Setting it by hand as below is dangerous. MIME tools from EE // do it safely. connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=\"cRaZy\""); // The text to post. // Note the field name, xml_query, specified explicitly. String postString = "--cRaZy\n" + "Content-Disposition: form-data; name=\"xml_query\"\n" + "Content-Type: text/xml\n" + "\n" + "\n" + " \n" + " \n" + "\n\n" + "--cRaZy--\n"; OutputStream outStream = connection.getOutputStream(); OutputStreamWriter out = new OutputStreamWriter(outStream); System.out.println( "About to post the query." ) ; out.write(postString); out.flush(); System.out.print( "Posted. The return code is " ) ; System.out.println( connection.getResponseCode() ); System.out.println( "The output from the server is:" ); int c; InputStream inStream = connection.getInputStream(); while ((c = inStream.read()) > 0) { System.out.print( (char)c ); } System.out.println( "" ); // The streams will close themselves on leaving the scope } }