commons httpclient 工具类 代码总结

发送Get请求
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 );
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值