package com.mindprod.http;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
/**
* 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.0 2009-02-20 - major refactoring. separate setParms and setPostParms. new send method. Post can have both types of parm.
* @since 1998
*/
public final class Get extends Http
{
/**
* constructor
*/
public Get()
{
}
/**
* Send a form full of data to the CGI host using GET.
*
* @param url complete URL including get parms, pre-encoded.
* @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, String encoding )
{
try
{
responseCode = DEFAULT_RESPONSE_CODE;
responseMessage = DEFAULT_RESPONSE_MESSAGE;
final HttpURLConnection 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 );
return processResponse( encoding, urlc );
}
catch ( ClassCastException e )
{
return null;
}
catch ( IOException e )
{
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, String encoding )
{
try
{
responseCode = DEFAULT_RESPONSE_CODE;
responseMessage = DEFAULT_RESPONSE_MESSAGE;
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 )
{
return null;
}
catch ( IOException e )
{
return null;
}
}
}