Jetty不仅可以拥有方便的服务器组件,也能很方便地发起Http请求。
Jetty的HttpClient组件可以用来向web服务器发起HTTP请求,解析响应内容,它有下面一些特性,比起JRE自带的要强大很多
1,默认是异步,也可以指定为同步
2,默认是非阻塞连接,也可以指定阻塞connectors
3,支持SSL协议
4,支持HTTP代理
5,支持HTTP认证机制
6,可以详细配置超时,连接数,线程池等设置
HttpClient API提供回调接口异步地处理服务器端返回,一个请求响应单元被成为exchange,HttpClient API里面有两个主要的类
org.eclipse.jetty.client.HttpClient 管理线程池,代理设置,Http认证的配置,connector类型设置,ssl以及超时等其它于特定一个交互无关的所有配置。
org.eclipse.jetty.client.HttpExchange 需要编程继承的父类,控制特定一次Http请求的所有相关内容(请求头,请求方法,响应内容等等)
package test.jetty;
import java.io.IOException;
import org.eclipse.jetty.client.Address;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
public class TestClient{
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
client.setThreadPool(new QueuedThreadPool(50));
client.setMaxConnectionsPerAddress(10);
client.setTimeout(5000);
//启动之前先配置好
client.start();
HttpExchange exchange = new HttpExchange(){
@Override
public void onConnectionFailed(Throwable arg0) {
// TODO Auto-generated method stub
System.err.println("failed to connect web server");
}
@Override
public void onException(Throwable arg0) {
// TODO Auto-generated method stub
System.err.println("unknown error");
}
@Override
public void onExpire() {
// TODO Auto-generated method stub
System.err.println("timeout");
}
@Override
public void onRequestCommitted() throws IOException {
// TODO Auto-generated method stub
System.out.println("request commited");
}
@Override
public void onRequestComplete() throws IOException {
// TODO Auto-generated method stub
System.out.println("request complete");
}
@Override
public void onResponseComplete() throws IOException {
// TODO Auto-generated method stub
System.out.println("response complete");
}
@Override
public void onResponseContent(Buffer arg0) throws IOException {
// TODO Auto-generated method stub
System.out.println("response content");
System.out.println(arg0.toString());
}
@Override
public void onResponseHeader(Buffer arg0, Buffer arg1) throws IOException {
// TODO Auto-generated method stub
System.out.println("response header");
System.out.println(arg0.toString() + " " + arg1.toString());
}
@Override
public void onResponseStatus(Buffer arg0, int arg1, Buffer arg2)
throws IOException {
// TODO Auto-generated method stub
System.out.println("response status");
System.out.println(arg0.toString() + " " + arg1 + " " + arg2.toString());
}
@Override
public void onRetry() {
// TODO Auto-generated method stub
System.out.println("retry request");
}
@Override
public void onResponseHeaderComplete() throws IOException {
// TODO Auto-generated method stub
System.out.println("response header complete");
}
};
exchange.setMethod("GET");
exchange.setAddress(new Address("www.baidu.com",80));
exchange.setRequestURI("/");
//client.send会立即返回,exchange会被分发给线程池去处理,
client.send(exchange);
System.out.println("send");
}
}
运行结果
send request commited request complete response status HTTP/1.1 200 OK response header Date Sun, 23 Dec 2012 10:14:30 GMT response header Server BWS/1.0 response header Content-Length 9777 response header Content-Type text/html;charset=gbk response header Cache-Control private response header Expires Sun, 23 Dec 2012 10:14:30 GMT response header Set-Cookie BAIDUID=9E28256B347526A1BE2FA301AE032CE7:FG=1; expires=Sun, 23-Dec-42 10:14:30 GMT; path=/; domain=.baidu.com response header P3P CP=" OTI DSP COR IVA OUR IND COM " response header Connection Keep-Alive response header complete response content …content… response complete
上面的示例代码演示了异步请求,下面的代码演示如何做同步请求。
package test.jetty;
import org.eclipse.jetty.client.Address;
import org.eclipse.jetty.client.ContentExchange;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
public class TestClient{
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
client.setThreadPool(new QueuedThreadPool(50));
client.setMaxConnectionsPerAddress(10);
client.setTimeout(5000);
//启动之前先配置好
client.start();
ContentExchange exchange = new ContentExchange();
exchange.setMethod("GET");
exchange.setAddress(new Address("www.baidu.com",80));
exchange.setRequestURI("/");
//client.send会立即返回,exchange会被分发给线程池去处理,
client.send(exchange);
System.out.println("send");
//阻塞等待
exchange.waitForDone();
System.out.println(exchange.getResponseStatus());
//System.out.println(exchange.getResponseContentBytes());
System.out.println(exchange.getResponseContent());
}
}
还不知道在同步的情况下ContentExchange的哪个方法可以获取到响应头信息。。。