org.apache.commons.httpclient.HttpClient hc = new
org.apache.commons.httpclient.HttpClient();
hc.getHostConfiguration().setHost("localhost", 80);
org.apache.commons.httpclient.methods.MultipartPostMethod mpm = new
org.apache.commons.httpclient.methods.MultipartPostMethod("/upload.asp");
mpm.addParameter("username", "username");
mpm.addParameter("file1", "文件名.txt", new java.io.File("c://boot.ini"));
mpm.addParameter("file2", "再来一个文件.txt", new java.io.File("c://boot.ini"));
hc.executeMethod(mpm);
System.out.println(mpm.getResponseBodyAsString());
org.apache.commons.httpclient.HttpClient hc = new
org.apache.commons.httpclient.HttpClient();
hc.getHostConfiguration().setHost("localhost", 80);
org.apache.commons.httpclient.methods.MultipartPostMethod mpm = new
org.apache.commons.httpclient.methods.MultipartPostMethod("/upload.asp");
mpm.addParameter("username", "username");
mpm.addParameter("file1", "文件名.txt", new java.io.File("c://boot.ini"));
mpm.addParameter("file2", "再来一个文件.txt", new java.io.File("c://boot.ini"));
hc.executeMethod(mpm);
System.out.println(mpm.getResponseBodyAsString());
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class FormLoginDemo {
static final String LOGON_STR = " localhost " ;
static final int LOGON_PORT = 8080 ;
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_STR, LOGON_PORT);
// 模拟登录页面login.jsp->main.jsp
PostMethod post = new PostMethod( " /a.jsp " );
NameValuePair name = new NameValuePair( " name " , " Id " );
NameValuePair pass = new NameValuePair( " password " , " Id " );
post.setRequestBody( new NameValuePair[] {name, pass} );
int status = client.executeMethod(post);
System.out.println(post.getResponseBodyAsString());
post.releaseConnection();
// 查看cookie 信息
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie[] cookies = cookiespec.match(LOGON_STR, LOGON_PORT, " / " , false , client.getState().getCookies());
if (cookies.length == 0 ) {
System.out.println( " none " );
} else {
for ( int i = 0 ; i < cookies.length; i ++ ) {
System.out.println(cookies[i].toString());
}
}
// 访问所需的页面main2.jsp
GetMethod get = new GetMethod( " /b.jsp " );
client.executeMethod(get);
System.out.println(get.getResponseBodyAsString());
get.releaseConnection();
}
}
针对采用HttpClient 文件上传 的其实和一般上传一样,将文件以流的形式作为请求的信息RequestBody 体即可
必须注意中文问题:
关于httpclient上传文件是中文名称的处理
在一个项目中,上传文件采用httpclient来post文件,在测试中发现如果文件是中文名称,上传的文件是乱码
经过跟踪发现,原来在httpclient中进行了编码,为ASCII,所以为乱码
org\apache\commons\httpclient\util包下EncodingUtil.Java
/**
*ConvertsthespecifiedstringtobytearrayofASCIIcharacters.
*
*@paramdatathestringtobeencoded
*@returnThestringasabytearray.
*
*@since3.0
*/
publicstaticbyte[]getAsciiBytes(finalStringdata){
if(data==null){
thrownewIllegalArgumentException("Parametermaynotbenull");
}
http://www.mscto.com
try{
returndata.getBytes("US-ASCII");
}catch(UnsupportedEncodingExceptione){
thrownewHttpClientError("HttpClientrequiresASCIIsupport");
}
}
解决方法:
1在接收端处理ascii数据
2重新编译httpclient包,即更改上面的方法,改为iso8859-1或utf-8
这样可以解决中文的问题
采用httpclient是一个很好的方法,可以给你其他的系统post数据,好像Delphi中的indy控件,