发送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 );
分享到:
相关推荐
httpclient 老版本依赖的jar包 包含三个文件:commons-codec-1.3.jar,commons-httpclient-3.0.jar,commons-logging-1.0.4.jar,
然而随着项目的升级和架构的调整,很多以前常用的类和方法都已被打上了@Deprecated注解,作为一个有代码洁癖的程序猿,我们也有必要升级一下工具类,让代码更加整洁。 另外在项目中正好需要访问https协议的接口,而...
模拟浏览器方法网页用到的工具类,HttpClient,HttpMethod
http工具类:package com.tpl.util; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import ...
开发jar包,包括:commons-codec-1.7.jar、commons-logging-1.1.1.jar、httpclient-4.2.2.jar、httpcore-4.2.2.jar
下载这个工具类,然后直接调用createHtmlPage方法就可以了。最关键一点是,网上的方法没有一个解决了乱码问题,费了我不少功夫。
Commons-Lang 提供了许多许多通用的工具类集,提供了一些java.lang中类的扩展功能 Latka Commons-Latka 是一个HTTP 功能测试包,用于自动化的QA,验收和衰减测试. Launcher Launcher 组件是一个交叉平台的Java 应用...
java开发工具类httpclient-4.5.jar,httpcore-4.4.6.jar,commons-logging-1.2.jar,commons-httpclient-3.1.jar,commons-logging-1.2.jar,commons-codec-1.9.jar,commons-collections-3.2.2.jar,commons-...
org.apache.http.impl.client.HttpClients 与 org.apache.commons.httpclient.HttpClient目前后者已被废弃,apache已不再支持。 一般而言,使用HttpClient均需导入httpclient.jar与httpclient-core.jar2个包。 1.4...
HTTP网络客户端请求基础工具类,可发送常见的GET、POST等请求方式,中文编码已处理,基于SSL连接,依赖的jar包有:commons-lang-2.6.jar、httpclient-4.3.2.jar、httpcore-4.3.1.jar、commons-io-2.4.jar
8.commons-Codec.jar包:处理常用的编码方法的工具类包 例如DES、SHA1、MD5、Base64等. 9.commons-Configuration.jar:一个java应用程序的配置管理类库 10.commons-Betwixt.jar:XML与Java对象之间相互转换。 11....
commons-beanutils-1.7.0, commons-collections-3.2.1, commons-httpclient-3.1, commons-lang-2.3, commons-logging-1.1.1, ezmorph-1.0.3, json-lib-2.2.3-jdk15
2、commons-httpclient-3.1.jar和commons-codec-1.4.jar:用于http调用 【其他文件】 1、schema目录下TSK_AirFareFlightShop.xsd描述的是国际运价FlightShopping的schema文档 2、sample目录下的文件,是请求和返回...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...
SimpleCache 是一个简单易用的java缓存工具,用来简化缓存代码的编写,让你摆脱单调乏味的重复工作!1. 完全透明的缓存支持,对业务代码零侵入 2. 支持使用Redis和Memcached作为后端缓存。3. 支持缓存数据分区规则的...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...
|--taotao-common --- 通用组件、工具类 |--taotao-manage -- 后台系统 |--com.taotao.manage.web |--com.taotao.manage.service |--com.taotao.manage.mapper |--com.taotao.manage.pojo 3.4. 创建...
SimpleCache 是一个简单易用的java缓存工具,用来简化缓存代码的编写,让你摆脱单调乏味的重复工作!1. 完全透明的缓存支持,对业务代码零侵入 2. 支持使用Redis和Memcached作为后端缓存。3. 支持缓存数据分区规则的...