发送Get请求
设置相关Header信息
发送POST请求
POST请求发送一个文件
POST请求上传多个文件
访问需要认证的系统
请求添加Cookie
public class TestA {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
HttpClient client = new HttpClient( );
String url = "http://www.discursive.com/cgi-bin/jccook/param_list.cgi";
HttpMethod method = new GetMethod( url );
//设置请求参数同时给参数编码
method.setQueryString(URIUtil.encodeQuery("test1=O Reilly&blah=Whoop"));
System.out.println( "With Query String: " + method.getURI( ) );
client.executeMethod( method );
System.out.println( "Response:\n " + method.getResponseBodyAsString( ) );
method.releaseConnection( );
}
}
设置相关Header信息
private void setHeaders(HttpMethod method) {
method.setRequestHeader(new Header("If-None-Match", entityTag ) );
method.setRequestHeader(new Header("If-Modified-Since",
lastModified ) );
}
发送POST请求
HttpClient client = new HttpClient( );
// Create POST method
String url = "http://www.discursive.com/cgi-bin/jccook/param_list.cgi";
PostMethod method = new PostMethod( url );
// Set parameters on POST
method.setParameter( "test1", "Hello World" );
method.addParameter( "test2", "This is a Form Submission" );
method.addParameter( "Blah", "Whoop" );
method.addParameter( new NameValuePair( "Blah", "Whoop2" ) );
// Execute and print response
client.executeMethod( method );
String response = method.getResponseBodyAsString( );
System.out.println( response );
method.releaseConnection( );
POST请求发送一个文件
HttpClient client = new HttpClient( );
// Create POST method
String weblintURL = "http://ats.nist.gov/cgi-bin/cgi.tcl/echo.cgi";
PostMethod method = new PostMethod( weblintURL );
File file = new File( "project.xml" );
method.setRequestBody( new FileInputStream( file ) );
method.setRequestContentLength( (int)file.length( ) );
// Execute and print response
client.executeMethod( method );
String response = method.getResponseBodyAsString( );
System.out.println( response );
method.releaseConnection( );
POST请求上传多个文件
HttpClient client = new HttpClient( );
// Create POST method
String weblintURL = "http://ats.nist.gov/cgi-bin/cgi.tcl/echo.cgi";
MultipartPostMethod method =
new MultipartPostMethod( weblintURL );
File file = new File( "data", "test.txt" );
File file2 = new File( "data", "sample.txt" );
method.addParameter("test.txt", file );
method.addPart( new FilePart( "sample.txt", file2, "text/plain",
"ISO-8859-1" ) );
// Execute and print response
client.executeMethod( method );
String response = method.getResponseBodyAsString( );
System.out.println( response );
method.releaseConnection( );
访问需要认证的系统
HttpClient client = new HttpClient( );
HttpState state = client.getState( );
// Set credentials on the client
Credentials credentials =
new UsernamePasswordCredentials( "testuser", "crazypass" );
state.setCredentials( null, null, credentials );
String url = "http://www.discursive.com/jccook/auth/";
HttpMethod method = new GetMethod( url );
client.executeMethod( method );
String response = method.getResponseBodyAsString( );
System.out.println( response );
method.releaseConnection( );
请求添加Cookie
HttpClient client = new HttpClient( );
Cookie cookie = new Cookie(".discursive.com", "test_cookie",
"hello", "/", null, false );
client.getState( ).addCookie( cookie );