public String baseGetMethod2(String link) {
HttpParams params = new BasicHttpParams();
// HTTP 协议的版本,1.1/1.0/0.9
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
// 字符集
// HttpProtocolParams.setContentCharset(params, "UTF-8");
// 伪装的浏览器类型
// IE7 是
// Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)
//
// Firefox3.03
// Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3
//
// HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
HttpProtocolParams.setUseExpectContinue(params, true);
BasicHttpProcessor httpproc = new BasicHttpProcessor();
httpproc.addInterceptor(new RequestContent());
httpproc.addInterceptor(new RequestTargetHost());
httpproc.addInterceptor(new RequestConnControl());
httpproc.addInterceptor(new RequestUserAgent());
httpproc.addInterceptor(new RequestExpectContinue());
httpproc.addInterceptor(new RequestAddCookies());
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
HttpContext context = new BasicHttpContext(null);
HttpHost host = new HttpHost("innopac.lib.xjtu.edu.cn", 80);
DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
try {
String targets = "/patroninfo~S3*chx/1004896/items";
if (!conn.isOpen()) {
Socket socket = new Socket(host.getHostName(), host.getPort());
conn.bind(socket, params);
}
BasicHttpRequest request = new BasicHttpRequest("GET", targets);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
context.setAttribute(ClientContext.COOKIE_STORE, loginCookieStore);
// params.setParameter("Cookie", parseCookieToString());
// request.setParams(params);
// request.setHeader("Cookie", parseCookieToString());
httpexecutor.preProcess(request, httpproc, context);
HttpResponse response = httpexecutor.execute(request, conn, context);
response.setParams(params);
httpexecutor.postProcess(response, httpproc, context);
// 返回的文件头信息
Header[] hs = response.getAllHeaders();
for (Header h : hs) {
System.out.println(h.getName() + ":" + h.getValue());
}
// 输出主体信息
System.out.println(EntityUtils.toString(response.getEntity()));
System.out.println("==============");
if (!connStrategy.keepAlive(response, context)) {
conn.close();
} else {
System.out.println("Connection kept alive...");
}
} catch (Exception e) {
} finally {
try {
conn.close();
} catch (Exception e2) {
}
}
return null;
}
posted on 2011-07-09 22:08
SIMONE 阅读(977)
评论(0) 编辑 收藏 所属分类:
JAVA