📄 post.java
字号:
package com.mindprod.http;
import java.io.IOException;
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.9 2008-08-22 support accept-charset, accept-encoding and accept-language. Fix bugs in gzip support.
*/
public final class Post extends Http
{
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* Constructor
*/
public Post()
{
}
/**
* Send a form full of data to the CGI host using POST
*
* @param url URL of the website, including host, path but not the parms.
* only http:
* @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", "WeakerAccess" } )
public String post( URL url,
String encoding,
String... parms )
{
try
{
// defaults
responseCode = DEFAULT_RESPONSE_CODE;
responseMessage = DEFAULT_RESPONSE_MESSAGE;
// prepare parms
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"
/* encoding */ ) );
sb.append( '=' );
sb.append( URLEncoder.encode( parms[ i + 1 ], "UTF-8"
/* encoding */ ) );
}
final byte[] encodedParms =
sb.toString().getBytes( "UTF-8"/* encoding */ );
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" );
setStandardProperties( urlc );
// could set Referrer: here
urlc.setRequestProperty( "Content-Length",
Integer.toString( encodedParms.length )
/* in bytes */ );
urlc.setRequestProperty( "Content-Type",
"application/x-www-form-urlencoded" );
urlc.connect();
final OutputStream os = urlc.getOutputStream();
// parms are the data content.
os.write( encodedParms );
os.close();
return processResponse( encoding, urlc );
}
catch ( ClassCastException e )
{
// was not an http: url
return null;
}
catch ( IOException e )
{
return null;
}
}
/**
* Send a form full of data to the CGI host using POST
*
* @param host domain of the website. no lead http:
* @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 String post( String host,
int port,
String encoding,
String action,
String... parms )
{
try
{
// defaults
responseCode = 404;
responseMessage = "not HTTP";
// URL will encode target and parms.
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;
}
}// end post
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -