HttpClient3.1高级应用
1. 下载地址:http://hc.apache.org/downloads.cgi
2. GET方法
public class MyHttpClient extends HttpClient {
static HttpClient myHttp = null;
static int number = 0;
public static HttpClient getHttpClient() {
if (myHttp == null || number > 500) {
myHttp = new HttpClient();
myHttp.getHttpConnectionManager().getParams()
.setConnectionTimeout(60000);
number = 0;
}
number++;
return myHttp;
}
}
public static String doGet(String url, String setCode, String readCode) {
String sTotalString = "";
setCode = (setCode == null || "".equals(setCode)) ? "gb2312" : setCode;
readCode = (readCode == null || "".equals(readCode)) ? "gb2312"
: readCode;
GetMethod get = new GetMethod(url);
get.getParams().setHttpElementCharset(setCode);
get.getParams().setCredentialCharset(setCode);
get.getParams().setContentCharset(setCode);
try {
HttpClient httpClient = MyHttpClient.getHttpClient();
int statusCode = httpClient.executeMethod(get);
if (statusCode != HttpStatus.SC_OK) {
return "查询失败!";
}
byte[] responseBody = get.getResponseBody();
ByteArrayInputStream is = new ByteArrayInputStream(responseBody);
BufferedReader l_reader = new BufferedReader(new InputStreamReader(
is, readCode));
String sCurrentLine = "";
while ((sCurrentLine = l_reader.readLine()) != null) {
sTotalString += sCurrentLine;
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
get.releaseConnection();
sTotalString = (sTotalString == null) ? "查询失败" : sTotalString;
}
return sTotalString;
}
3. POST方法
public static String doPost(String url, NameValuePair[] param, String setCode, String readCode) {
String sTotalString = "";
setCode = (setCode == null || "".equals(setCode)) ? "gb2312" : setCode;
readCode = (readCode == null || "".equals(readCode)) ? "gb2312"
: readCode;
PostMethod post = new PostMethod(url);
post.getParams().setHttpElementCharset(setCode);
post.getParams().setCredentialCharset(setCode);
post.getParams().setContentCharset(setCode);
post.setRequestBody(param);
try {
HttpClient httpClient = MyHttpClient.getHttpClient();
int statusCode = httpClient.executeMethod(post);
if (statusCode != HttpStatus.SC_OK) {
return "查询失败!";
}
byte[] responseBody = post.getResponseBody();
ByteArrayInputStream is = new ByteArrayInputStream(responseBody);
BufferedReader l_reader = new BufferedReader(new InputStreamReader(
is, readCode));
String sCurrentLine = "";
while ((sCurrentLine = l_reader.readLine()) != null) {
sTotalString += sCurrentLine;
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
post.releaseConnection();
sTotalString = (sTotalString == null) ? "查询失败" : sTotalString;
}
return sTotalString;
}
4. 下载验证码图片
public static void getYzm(String url, String Path) {
PostMethod post = new PostMethod(url);
try {
HttpClient httpClient = MyHttpClient.getHttpClient();
int statusCode = httpClient.executeMethod(post);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
InputStream i = post.getResponseBodyAsStream();
if (i != null) {
writePic(i, Path);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void download(String url, String path) {
HttpClient client = MyHttpClient.getHttpClient();
GetMethod get = new GetMethod(url);
try {
int statusCode = client.executeMethod(get);
try {
writePic(get.getResponseBody(), path);
} catch (Exception e) {
e.printStackTrace();
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void writePic(byte[] responseBody, String Path)
throws Exception {
ByteArrayInputStream is = new ByteArrayInputStream(responseBody);
BufferedInputStream in = new BufferedInputStream(is);
FileOutputStream fo = new FileOutputStream(Path);
BufferedOutputStream out = new BufferedOutputStream(fo);
byte[] buf = new byte[1024];
int len = in.read(buf);
while (len != -1) {
out.write(buf, 0, len);
len = in.read(buf);
}
out.close();
fo.close();
in.close();
}
public static void writePic(InputStream i, String Path) throws Exception {
BufferedInputStream in = new BufferedInputStream(i);
FileOutputStream fo = new FileOutputStream(Path);
BufferedOutputStream out = new BufferedOutputStream(fo);
byte[] buf = new byte[1024];
int len = in.read(buf);
while (len != -1) {
out.write(buf, 0, len);
len = in.read(buf);
}
out.close();
fo.close();
in.close();
}
5. 疑难问题解决
(1) 针对某些不能返回数据问题的解决方法
/**
* 针对不能返回数据问题的POST提交
* @param mainUrl 发票查询主页面URL
* @param url 提交查询的URL
* @param param
* @param setCode
* @param readCode
* @return
* 先到主页面访问一次,获取到COOKIES,将COOKIES设置到提交查询的请求中,在执行提交查询的访问。
*/
public static String doPost(String mainUrl, String url, NameValuePair[] param,
String setCode, String readCode) {
PostMethod method = new PostMethod(mainUrl);
try {
HttpClient httpClient = MyHttpClient.getHttpClient();
int statusCode = httpClient.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
return "查询失败!";
}
Cookie[] cookies = httpClient.getState().getCookies();
httpClient.getState().addCookies(cookies);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
method.releaseConnection();
}
return doPost(url, param, setCode, readCode);
}
(2) 针对不能获取到验证码图片的问题
/**
* 针对有防盗链机制的网站获取验证码图片
* @param list HTTP HEADER信息
* @param url
* @param path
* @return
*/
public static void getYzm(List<Header> headers, String url, String path) {
HttpClient httpClient = MyHttpClient.getHttpClient();
httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
download(url, path);
}