com.jfw.web.util
Class ExecuteUrl

java.lang.Object
  extended by com.jfw.web.util.ExecuteUrl

public final class ExecuteUrl
extends java.lang.Object

Used for open an http connection.

Open connection via get

 //setting url and parameters
 ExecuteUrl url = new ExecuteUrl("http://www.mysite.com", "myPage.do?username=usr&password=pwd");

 //get all text returned from myPage.do
 String output = url.readAll();

 //closing streams
 url.close();
Open connection via post
 //setting url
 ExecuteUrl url = new ExecuteUrl("http://www.mysite.com", "myPage.do");

 //setting parameters
 url.write("username", "usr");
 url.write("password", "pwd");
 url.flush();

 //get all text returned from myPage.do
 String output = url.readAll();

 //closing streams
 url.close();


Constructor Summary
ExecuteUrl(java.lang.String page)
          Connect to a page via get.
ExecuteUrl(java.lang.String proxy, int proxyPort, java.lang.String page, java.util.HashMap requestParams)
          Connect to a page via get using a proxy and request parameters.
ExecuteUrl(java.lang.String host, java.lang.String page)
          Connect to a page via get.
ExecuteUrl(java.lang.String host, java.lang.String page, java.util.HashMap requestParams)
          Connect to a page via get using request parameters.
ExecuteUrl(java.lang.String host, java.lang.String page, int port)
          Connect to a page via post.
 
Method Summary
 void close()
          Close opened stream and the connection.
 void finalize()
          Call method close() and super.finalize().
 void flush()
          Flush the socket, if post method is used, or the output stream.
 byte[] read()
          Read input stream.
 java.lang.String read(int bytesToRead)
          Read input stream.
 java.lang.String readAll()
          Read all data of input stream.
 void write(java.lang.String name, java.lang.String value)
          Add a post parameter.
 void write(java.lang.String name, java.lang.String value, java.lang.String encode)
          Add a post parameter.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExecuteUrl

public ExecuteUrl(java.lang.String host,
                  java.lang.String page,
                  int port)
           throws JFWGenericException
Connect to a page via post.

Parameters:
host - host name.
page - page to connect.
port - remote server port.
Throws:
JFWGenericException

ExecuteUrl

public ExecuteUrl(java.lang.String page)
           throws JFWGenericException
Connect to a page via get.

Parameters:
page - the url to connect.
Throws:
JFWGenericException

ExecuteUrl

public ExecuteUrl(java.lang.String host,
                  java.lang.String page)
           throws JFWGenericException
Connect to a page via get.

Parameters:
host - host name.
page - page to connect.
Throws:
JFWGenericException

ExecuteUrl

public ExecuteUrl(java.lang.String proxy,
                  int proxyPort,
                  java.lang.String page,
                  java.util.HashMap requestParams)
           throws JFWGenericException
Connect to a page via get using a proxy and request parameters.

Parameters:
proxy - proxy to use.
proxyPort - proxy port. If -1 use default port.
page - page to connect.
requestParams - request parameters, ex. Authorization. Set null for no parameters.
Throws:
JFWGenericException

ExecuteUrl

public ExecuteUrl(java.lang.String host,
                  java.lang.String page,
                  java.util.HashMap requestParams)
           throws JFWGenericException
Connect to a page via get using request parameters.

Parameters:
host - host name.
page - page to connect.
requestParams - request parameters, ex. Authorization.
Throws:
JFWGenericException
Method Detail

read

public java.lang.String read(int bytesToRead)
                      throws JFWGenericException
Read input stream.

Parameters:
bytesToRead - the number if bytes to read.
Returns:
ritorna the readed bytes as a String.
Throws:
JFWGenericException

read

public byte[] read()
            throws JFWGenericException
Read input stream. You can call this method more times for read all the content returned by the executed url

Returns:
the readed bytes. When not exist more data to read then a null object is returned.
Throws:
JFWGenericException

readAll

public java.lang.String readAll()
                         throws JFWGenericException
Read all data of input stream. The reading will block until all data are readed.

Returns:
the data send by the connected url.
Throws:
JFWGenericException

write

public void write(java.lang.String name,
                  java.lang.String value,
                  java.lang.String encode)
           throws java.lang.Exception
Add a post parameter. The url encoding is done by the method.

Parameters:
name - the name of the parameter.
value - the value of the parameter.
encode - the encoding to use. If null then UTF-8 will be used.
Throws:
java.lang.Exception

write

public void write(java.lang.String name,
                  java.lang.String value)
           throws java.lang.Exception
Add a post parameter. The url encoding is done by the method. The default encoding is given by key url_encode of jfw.properties: if this key is not defined then UTF-8 is used.

Parameters:
name - the name of the parameter.
value - the value of the parameter.
Throws:
java.lang.Exception

flush

public void flush()
           throws java.lang.Exception
Flush the socket, if post method is used, or the output stream.

Throws:
java.lang.Exception

close

public void close()
Close opened stream and the connection.


finalize

public void finalize()
              throws java.lang.Throwable
Call method close() and super.finalize().

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable