package com.mindprod.http;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
/**
* simulates a browser posting a form to CGI via GET.
* <p/>
* See com.mindprod.submitter for sample code to use this class.
*
* @author Roedy Green, Canadian Mind Products
* @version 2.3 2010-11-14 new method setInstanceFollowRedirects
* @since 1998
*/
public final class Get extends Http
{
/**
* the connection. null if no connection in progess
*/
HttpURLConnection urlc;
/**
* constructor
*/
public Get()
{
}
/**
* disconnect/abort the current connection
*/
public void disconnect()
{
if ( urlc != null )
{
urlc.disconnect();
urlc = null;
}
}
/**
* Send a form full of data to the CGI host using GET.
* Note. This method ignores any setParms.
*
* @param url complete URL including get parms, pre-encoded, http: or https:.
* @param encoding encoding of the byte stream result, usually UTF-8 or or ISO-8859-1.
*
* @return CGI host's response with headers and embedded length fields stripped.
* @see com.mindprod.http.Get
*/
@SuppressWarnings( { "UnusedAssignment", "MethodNamesDifferingOnlyByCase", "WeakerAccess" } )
public String send( URL url, Charset encoding )
{
try
{
init();
this.url = url;
urlc = ( HttpURLConnection ) url.openConnection();
urlc.setAllowUserInteraction( false );
urlc.setDoInput( true );
urlc.setDoOutput( false );// nothing beyond original request
urlc.setUseCaches( false );
urlc.setRequestMethod( "GET" );
setStandardProperties( urlc );
final String result = connectAndProcessResponse( encoding, urlc );
urlc = null;
return result;
}
catch ( ClassCastException e )
{
interruptResponseMessage = "Bug : not http/https : " + e.getMessage();
urlc = null;
return null;
}
catch ( IOException e )
{
interruptResponseMessage = e.getClass().getName() + " : " + e.getMessage();
urlc = null;
return null;
}
}
/**
* Send a form full of data to the CGI host using GET.
* Must have previously specified the parms with setParms.
*
* @param host host name of the website, Should be form:"mindprod.com", no lead http://.
* @param port -1 if default, 8081 for local echoserver.
* @param action action of form, page on website. Usually has a lead / and no trailing /.
* @param encoding encoding of the byte stream result, usually UTF-8 or or ISO-8859-1.
*
* @return CGI host's response with headers and embedded length fields stripped.
*/
@SuppressWarnings( { "UnusedAssignment", "MethodNamesDifferingOnlyByCase" } )
public String send( String host, int port, String action, Charset encoding )
{
try
{
init();
URL url = new URI( "http",
null,
host,
port,
action,
null,
null ).toURL();
url = new URL( url.toString() + getEncodedParms( encoding ) );
return send( url, encoding );
}
catch ( URISyntaxException e )
{
interruptResponseMessage = "Bad URI/URL";
return null;
}
catch ( IOException e )
{
interruptResponseMessage = e.getClass().getName() + " : " + e.getMessage();
return null;
}
}
}