package com.mindprod.http;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
/**
* simulates a browser posting a form to CGI via POST. See com.mindprod.submitter for sample code to use this class.
*
* @author Roedy Green, Canadian Mind Products may be copied and used freely for any purpose but military.
* @version 1.6 2008-01-14 add gzip option on read
*/
public final class Post
{
/**
* Allow 50 seconds to connect
*/
private static final int CONNECT_TIMEOUT = 50 * 1000;
/**
* Allow 40 seconds for a read to go without progress
*/
private static final int READ_TIMEOUT = 40 * 1000;
/**
* responseCode from most recent post
*/
private static int responseCode;
/**
* 40 seconds TIMEOUT = 40,000 ms.
*/
private static final int TIMEOUT = 40000;
/**
* responseCode from most recent post
*
* @return responseCode
*/
public static int getResponseCode()
{
return responseCode;
}
/**
* Send a formful of data to the CGI host using POST
*
* @param url URL of the website, including host, path but not the parms.
* @param parms alternating parm value without = or ? not urlEncoded.
* @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 static String post( URL url,
String encoding,
String... parms )
{
try
{
final StringBuilder sb = new StringBuilder( 200 );
for ( int i = 0; i < parms.length - 1; i += 2 )
{
if ( i != 0 )
{
sb.append( "&" );
}
sb.append( URLEncoder.encode( parms[ i ], "UTF-8"
) );
sb.append( '=' );
sb.append( URLEncoder.encode( parms[ i + 1 ], "UTF-8"
) );
}
final byte[] encodedParms =
sb.toString().getBytes( "UTF-8");
final HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection();
urlc.setAllowUserInteraction( false );
urlc.setDoInput( true );
urlc.setDoOutput( true );// parms at the tail of this
urlc.setUseCaches( false );
urlc.setRequestMethod( "POST" );
urlc.setConnectTimeout( CONNECT_TIMEOUT );
urlc.setReadTimeout( READ_TIMEOUT );
urlc.setRequestProperty( "Content-Length",
Integer.toString( encodedParms.length )
);
urlc.setRequestProperty( "Content-Type",
"application/x-www-form-urlencoded" );
urlc.connect();
final OutputStream os = urlc.getOutputStream();
os.write( encodedParms );
os.close();
responseCode = urlc.getResponseCode();
int estimatedLength = urlc.getContentLength();
if ( estimatedLength < 0 )
{
estimatedLength = 32 * 1024;
}
final InputStream is = urlc.getInputStream();
final boolean gzipped = "gzip".equals( urlc.getContentEncoding() );
String result = Read.readStringBlocking( is,
estimatedLength,
TIMEOUT,
gzipped,
encoding );
is.close();
urlc.disconnect();
return result;
}
catch ( IOException e )
{
return null;
}
}
/**
* Send a formful of data to the CGI host using POST
*
* @param host URL of the website
* @param action action of form, page on website. Usually has a lead /.
* @param parms alternating parm value without = or ? not urlEncoded.
* @param port -1 if default, 8081 for local echoserver.
* @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 static String post( String host,
int port,
String encoding,
String action,
String... parms )
{
try
{
URL url = new URI( "http",
null,
host,
port,
action,
null,
null ).toURL();
return post( url, encoding, parms );
}
catch ( URISyntaxException e )
{
return null;
}
catch ( IOException e )
{
return null;
}
}
/**
* Static only. Prevent instantiation.
*/
private Post()
{
}
}