|
NOTA: La traducción de esta documentación es un esfuerzo personal y voluntario, no es un documento oficial de Sun Microsystems
ni Oracle ni está patrocinado por ninguna de estas empresas. Los documentos originales (en inglés) están disponibles en:
http://java.sun.com/docs/books/tutorial/.
Dirija cualquier comentario, petición, felicitación, etc. a tutorialesjava_@RROBA_codexion.com. |
Si desea ayudar a mantener en funcionamiento esta web, colaborar con la traducción de estos documentos o necesita que se traduzca algĂșn capĂtulo en concreto puede realizar una donación directa mediante Paypal:
|
After you've successfully created aURLobject, you can call theURLobject'sopenConnectionmethod to get aURLConnectionobject, or one of its protocol specific subclasses, e.g.java.net.HttpURLConnectionYou can use this
URLConnectionobject to setup parameters and general request properties that you may need before connecting. Connection to the remote object represented by the URL is only initiated when theURLConnection.connectmethod is called. When you do this you are initializing a communication link between your Java program and the URL over the network. For example, you can open a connection to the Yahoo site with the following code:A newtry { URL yahoo = new URL("http://www.yahoo.com/"); URLConnection yahooConnection = yahoo.openConnection(); yahooConnection.connect(); } catch (MalformedURLException e) { // new URL() failed . . . } catch (IOException e) { // openConnection() failed . . . }URLConnectionobject is created every time by calling theopenConnectionmethod of the protocol handler for this URL.You are not always required to explicitly call the
connectmethod to initiate the connection. Operations that depend on being connected, likegetInputStream,getOutputStream, etc, will implicitly perform the connection, if necessary.Now that you've successfully connected to your URL, you can use the
URLConnectionobject to perform actions such as reading from or writing to the connection. The next section shows you how.