KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > net > URL

java.net
Class URL

java.lang.Object
  extended by java.net.URL
All Implemented Interfaces:
Serializable
See Also:
Top Examples, Source Code, URLDecoder, URLEncoder, URI.toURL(), toURI()

public boolean equals(Object obj)
See Also:
Hashtable, Object.hashCode()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getAuthority()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1445]Set URL cookie and Request Property
By bgbluocean { at } netscape { dot } net on 2005/05/31 18:36:55  Rate
This code: 
  
  
 String myURL = view.getHttpURL (  ) ; 
 pw.println ( "attempting to open URL " + myURL + BR_TAG  ) ; 
 java.net.URL targetURL = new java.net.URL ( myURL ) ;  
 pw.println ( "targetURL.getContent (  ) :"+    targetURL.getContent (  ) .toString (  )  ) ; 
  
  
 --gave me: 
 sun.net.www.http.KeepAliveStream@6bdb4851 
  
  
 ----------- 
 Here's some follow on code: 
 java.net.HttpURLConnection connection =  ( java.net.HttpURLConnection )  targetURL.openConnection  (  ) ; 
   
 String currentCookies = doc.getItemValueString (  "HTTP_COOKIE"  ) ; 
     
 // connection.setRequestMethod (  "GET"  ) ; // This is the default request method 
 connection.setRequestProperty ( "User-Agent", "Mozilla/4.0  ( compatible ) " ) ; // MSIE 6.0; WindowsNT 5.1; YComp 5 .0.0.0;.NET CLR 1.0.3705;.NET CLR 1.1.4322 )  
 connection.setRequestProperty ( "Accept-Language", "en-us" ) ; 
 connection.setRequestProperty ( "Accept", "*/*" ) ; 
 connection.setRequestProperty ( "Connection", "Keep-Alive" ) ; 
 connection.setRequestProperty ( "Cache-Control", "no-cache" ) ; 
 if (  currentCookies != null  )   {  
   connection.setRequestProperty ( "Cookie", currentCookies  ) ; 
  }  
   
 connection.setDoInput ( true ) ; 
 connection.setDoOutput ( false ) ; 
 connection.connect (  ) ;  
 pw.println  ( "Connection up and running" + BR_TAG ) ; 
 int responseCode = connection.getResponseCode (  ) ;  
 pw.println  (  responseCode + BR_TAG ) ; 
 pw.println  (  "mssg: ".concat ( connection.getResponseMessage (  )  )   + BR_TAG  ) ; 
 


public final Object getContent()
                        throws IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1722]How to read a web page
By Anonymous on 2006/03/12 01:24:52  Rate
URL testUrl = new URL ( path ) ; 
         URLConnection uc = testUrl.openConnection (  ) ; 
         if  ( uc == null )   {  
             throw new Exception ( "Got a null URLConnection object!" ) ; 
          }  
         InputStream is = uc.getInputStream (  ) ; 
         if  ( is == null )   {  
             throw new Exception ( "Got a null content object!" ) ; 
          }  
         StringBuffer putBackTogether = new StringBuffer (  ) ; 
         Reader r = new InputStreamReader ( is, "UTF-8" ) ; 
         char [  ]  cb = new char [ 2048 ] ; 
  
  
         int amtRead = r.read ( cb ) ; 
         while  ( amtRead  >  0 )   {  
             putBackTogether.append ( cb, 0, amtRead ) ; 
             amtRead = r.read ( cb ) ; 
          }  
         pageSource = putBackTogether.toString (  ) ; 
 


public final Object getContent(Class[] classes)
                        throws IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getDefaultPort()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getFile()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1362]_
By amoderidwan { at } hotmail { dot } com on 2005/03/28 08:27:32  Rate
i want to fetch the URL from a browser is it possible with the java.net.url class library. if so what are the steps please. thanks in advance for all.

public String getHost()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getPath()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getPort()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getProtocol()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getQuery()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getRef()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getUserInfo()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int hashCode()
See Also:
Hashtable, Object.equals(java.lang.Object)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public URLConnection openConnection()
                             throws IOException
See Also:
URLStreamHandler.openConnection(java.net.URL), URL(java.lang.String, java.lang.String, int, java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[164]Browse HTML pages
By Anonymous on 2005/03/15 17:54:45  Rate
try  {  
   URL url = new URL ( "http://www.KickJava.com" ) ; 
   URLConnection urlConnection = url.openConnection (  ) ; 
   BufferedReader htmlPage = new BufferedReader ( new InputStreamReader ( url.openStream (  )  )  ) ; 
  
  
   String line = ""; 
   while (  ( line = htmlPage.readLine (  )  )  != null )   {  
      //do something with the html line 
    }  
   htmlPage.close (  ) ; 
   urlConnection = null; 
  }  catch ( Exception e )   {  
   e.printStackTrace (  ) ; 
  } 


public URLConnection openConnection(Proxy proxy)
                             throws IOException
See Also:
URLStreamHandler.openConnection(java.net.URL, java.net.Proxy), URL(java.lang.String, java.lang.String, int, java.lang.String), UnsupportedOperationException, IllegalArgumentException, SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final InputStream openStream()
                             throws IOException
See Also:
URLConnection.getInputStream(), openConnection()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[249]Search on yahoo
By Adil Jamshaid adilsec { at } yahoo { dot } com on 2003/05/06 19:27:50  Rate
import java.io.*; 
 import java.net.*; 
  
  
  
 public class Yahoo 
  {  
  
  
  
 static String Searcher="http://search.yahoo.com/bin/search?p="; 
 /* 
 Type 
 C: > java Yahoo MySearch 
 for results on dos prompt 
  
  
 For multi letter search 
 C: > java Yahoo "My Search" 
  
  
  
 To get text file of results write on dos prompt 
 C: > java Yahoo MySearch  > mytextfile.txt 
 and similarly myhtmlfile.html for html page 
  
  
  
 Experiment 
 http://www.higherlevelastrology.com/horoscope.html?HOROSCOPE=horoscope.html&B1=H 
 ERE# 
  
  
 */
 
  
  
 public static void main  ( String args [  ]  )  {  
  
  
 Yahoo ya=new Yahoo (  ) ; 
 try {  
  
  
  
         ya.search ( args [ 0 ]  ) ; 
          } catch ( Exception e )  {  
                 System.out.println ( "Problem"+e ) ; 
          }  
  }  
  
  
  
 InputStream is; 
  
  
 public void search  ( String wd ) throws IOException {  
  
  
 URL url=new URL ( Searcher+wd ) ; 
 is=url.openStream (  ) ; 
  
  
 Reader rd=new InputStreamReader ( is ) ; 
 BufferedReader br=new BufferedReader ( rd ) ; 
 String tmp=br.readLine (  ) ; 
 while ( tmp!=null )  {  
  
  
         System.out.println ( tmp ) ; 
         tmp=br.readLine (  ) ; 
  
  
  
  }  
 is.close (  ) ; 
  
  
  }  
  
  
  
  }  
 


public boolean sameFile(URL other)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected void set(String protocol,
                   String host,
                   int port,
                   String file,
                   String ref)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected void set(String protocol,
                   String host,
                   int port,
                   String authority,
                   String userInfo,
                   String path,
                   String query,
                   String ref)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
See Also:
SecurityManager.checkSetFactory(), URL(java.lang.String, java.lang.String, int, java.lang.String), SecurityException, Error
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String toExternalForm()
See Also:
URLStreamHandler.toExternalForm(java.net.URL), URL(java.lang.String, java.lang.String, int, java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String toString()
See Also:
URLStreamHandler.toExternalForm(java.net.URL), URL(java.lang.String, java.lang.String, int, java.lang.String), Object
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public URI toURI()
          throws URISyntaxException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public URL(String spec)
    throws MalformedURLException
See Also:
URL(java.net.URL, java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1527]HTTP and FTP thorugh a proxy
By Anonymous on 2005/09/06 09:27:24  Rate
//HTTP thorugh a proxy, need to specify the following system properties 
  
  
 System.getProperties (  ) .put (  "proxySet", "true"  ) ; 
 System.getProperties (  ) .put (  "proxyHost", "proxy-host-name"  ) ; 
 System.getProperties (  ) .put (  "proxyPort", "8080"  ) ; 
  
  
 //If your proxies require a user to type in a username and password before Internet access is granted.  
 //You need to call setRequestProperty (  )  to manipulate the HTTP headers  
  
  
 URLConnection connection = url.openConnection (  ) ; 
 String password = "username:password"; 
 String encodedPassword = base64Encode (  password  ) ; 
 connection.setRequestProperty (  "Proxy-Authorization", encodedPassword  ) ; 
  
  
  
 //FTP through Proxy 
 System.getProperties (  ) .put (  "ftpProxySet", "true"  ) ; 
 System.getProperties (  ) .put (  "ftpProxyHost", "proxy-host-name"  ) ; 
 System.getProperties (  ) .put (  "ftpProxyPort", "8080"  ) ; 
  
  
 URL url = new URL ( "ftp://ftp.something.com"  ) ;


public URL(String protocol,
           String host,
           int port,
           String file)
    throws MalformedURLException
See Also:
URLStreamHandlerFactory.createURLStreamHandler( java.lang.String), URLStreamHandler, setURLStreamHandlerFactory( java.net.URLStreamHandlerFactory), System.getProperty(java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public URL(String protocol,
           String host,
           int port,
           String file,
           URLStreamHandler handler)
    throws MalformedURLException
See Also:
NetPermission, SecurityManager.checkPermission(java.security.Permission), URLStreamHandlerFactory.createURLStreamHandler( java.lang.String), setURLStreamHandlerFactory( java.net.URLStreamHandlerFactory), System.getProperty(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public URL(String protocol,
           String host,
           String file)
    throws MalformedURLException
See Also:
URL(java.lang.String, java.lang.String, int, java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public URL(URL context,
           String spec)
    throws MalformedURLException
See Also:
URLStreamHandler.parseURL(java.net.URL, java.lang.String, int, int), URL(java.lang.String, java.lang.String, int, java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public URL(URL context,
           String spec,
           URLStreamHandler handler)
    throws MalformedURLException
See Also:
URLStreamHandler.parseURL(java.net.URL, java.lang.String, int, int), URL(java.lang.String, java.lang.String, int, java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags