Using URLConnection : URL « Network Protocol « Java






Using URLConnection

      
/* From http://java.sun.com/docs/books/tutorial/index.html */

/*
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 *
 * -Redistribution in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class Reverse {
  public static void main(String[] args) throws Exception {

    if (args.length != 1) {
      System.err.println("Usage:  java Reverse " + "string_to_reverse");
      System.exit(1);
    }

    String stringToReverse = URLEncoder.encode(args[0]);

    URL url = new URL("http://java.sun.com/cgi-bin/backwards");
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);

    PrintWriter out = new PrintWriter(connection.getOutputStream());
    out.println("string=" + stringToReverse);
    out.close();

    BufferedReader in = new BufferedReader(new InputStreamReader(connection
        .getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null)
      System.out.println(inputLine);

    in.close();
  }
}
           
         
    
    
    
    
    
  








Related examples in the same category

1.Creating a URL with a single string.
2.Creating a URL With components
3.Converting Between a Filename Path and a URL
4.URL Constructor Test
5.URL Encode Test
6.Get URL Content
7.Get URL Parts
8.Read from a URL
9.Convert a URL to a URI
10.Converting Between a URL and a URI
11.Convert an absolute URI to a URL
12.URL Equality
13.Parsing a URL
14.URL Request
15.URL Get
16.A URL Retrieval Example
17.URL Reader
18.URL Connection ReaderURL Connection Reader
19.Parse URLParse URL
20.Resolve a relative URL
21.sends e-mail using a mailto: URLsends e-mail using a mailto: URL
22.Convert the absolute URI to a URL object
23.Convert URI to URL
24.Get parts of a url
25.Checks, whether the URL uses a file based protocol.
26.Add Parameter to URL
27.Returns the anchor value of the given URL
28.Extracts the file name from the URL.
29.Creates a relative url by stripping the common parts of the the url.
30.Checks, whether the URL points to the same service. A service is equal if the protocol, host and port are equal.
31.Extracts the base URL from the given URL by stripping the query and anchor part.
32.Returns true if the URL represents a path, and false otherwise.
33.Parse Port
34.Parse Host
35.Given a URL check if its a jar url(jar:!/archive) and if it is, extract the archive entry into the given dest directory and return a file URL to its location
36.check the validity of url pattern according to the spec.
37.A collection of File, URL and filename utility methods
38.Build Relative URL Path
39.Checks that the protocol://host:port part of two URLs are equal
40.Create valid URL from a system id
41.Extract URL File Name
42.Extract the URL page name from the given path
43.Get Domain Name
44.Get Locale From String
45.Get URL Last Modified
46.Get the name of the parent of the given URL path
47.Get the parent of the given URL path
48.Has URLContent Changed
49.Is URL a local file
50.Normalize an URL
51.Normalizes an URL
52.Resolve a relative URL string against an absolute URL string
53.ResourceBundle String manager
54.Save URL contents to a file
55.URL Path: standardize the creation of mutation of path-like structures
56.Utility class for building URLs
57.Add Default Port to a URL If Missing
58.Get Relative Path To URL
59.Download from a URL and save to a file