HttpClient 是用来写 http 客户端程序的非常好的包。
下面的代码初步展示了如何使用这个了写客户端。
我们没有对这个程序进行测试,这里仅供参考。[code]package base.helper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class HttpClientHelper {
private HttpClient client = null;
private String baseUrl;
private String encoder;
public HttpClientHelper(String baseUrl, String encoder) {
createClient();
this.baseUrl = baseUrl;
this.encoder = encoder;
}
private void createClient() {
System.getProperties().setProperty("httpclient.useragent",
"Mozilla/4.0");
client = new HttpClient(new MultiThreadedHttpConnectionManager());
}
/**
* Download the url as html String
*
* @param url
* the url to be download.
* @return
*/
public String html(String url) {
url = MiscHelper.toAbsoluteUrl(this.baseUrl, url);
GetMethod get = new GetMethod(url);
get.setRequestHeader("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
get.setFollowRedirects(true);
try {
client.executeMethod(get);
return MiscHelper.method(get, this.encoder);
} catch (Exception e) {
createClient();
e.printStackTrace();
} finally {
get.releaseConnection();
}
return null;
}
/**
* Download the url conent and save into the file. It can use to download
* images,pdf,flash etc.
*
* @param url
* @param file
* @return
*/
public boolean file(String url, File file) {
url = MiscHelper.toAbsoluteUrl(this.baseUrl, url);
GetMethod get = new GetMethod(url);
get.setRequestHeader("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
get.setFollowRedirects(true);
boolean result = true;
try {
client.executeMethod(get);
InputStream in = get.getResponseBodyAsStream();
FileOutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int count = -1;
while ((count = in.read(buffer)) != -1) {
out.write(buffer, 0, count);
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
createClient();
result = false;
} finally {
get.releaseConnection();
}
return result;
}
public boolean file(String url) {
int i = url.lastIndexOf('/');
String fileName = url.substring(i + 1);
return file(url, new File(fileName));
}
/**
-
Submit the form to the url.
-
@param url
-
@param params
-
@return
-
@throws HttpException
-
@throws IOException
*/
public String postForm(String url, List params)
throws HttpException, IOException {
url = MiscHelper.toAbsoluteUrl(this.baseUrl, url);
PostMethod authpost = new UTF8PostMethod(url);
authpost.setRequestHeader(“ContentType”,
“application/x-www-form-urlencoded;charset=UTF-8”);
// authpost.addRequestHeader(“Content-Type”,“Content-Type: text/html;
// charset=utf-8”);
// Prepare login parameters
NameValuePair postParams = new NameValuePair[params.size()];
params.toArray(postParams);
authpost.setRequestBody(postParams);
client.executeMethod(authpost);
authpost.releaseConnection();
int statuscode = authpost.getStatusCode();
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
|| (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
|| (statuscode == HttpStatus.SC_SEE_OTHER)
|| (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
Header header = authpost.getResponseHeader(“location”);
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals(“”))) {
newuri = “/”;
}
return newuri;
}
}
return null;}
public static class UTF8PostMethod extends PostMethod {
public UTF8PostMethod(String url) {
super(url);
}@Override public String getRequestCharSet() { // return super.getRequestCharSet(); return "UTF-8"; }
}
}[/code]