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的哪个方法可以获取到响应头信息。。。
相关推荐
这份"java软件开发——顶岗实习周记25篇.pdf"文档,很可能是某个学生或初入职场的开发者在Java实习期间所记录的工作与学习心得,提供了宝贵的实战经验与反思。 首先,Java作为全球最流行的编程语言之一,其语法严谨...
6. **后台服务器**:服务器端的实现可能基于Java的网络编程技术,如Socket编程,也可能使用了成熟的服务器框架如Tomcat或Jetty,以处理多用户的连接和消息传递。 7. **数据库连接**:SQLQuery1.sql文件暗示了项目与...
【标题】"基于Java的企业内部通信系统源代码"揭示了这个项目的核心——使用Java编程语言构建了一套适用于企业内部的通信解决方案。Java是一种广泛应用于企业级应用开发的强大、跨平台的语言,其稳定性和丰富的库支持...
《Web课程设计:房屋出租管理系统》 ...通过这个项目,学生不仅可以深入学习Java编程,还能掌握Web应用开发的流程和技巧,提高解决实际问题的能力。同时,这样的实践经历对于理解Web技术栈和提升职业素养具有重要意义。
从提供的文件名称列表来看,这包括了 Spring 1.2.6 版本的 jar 包,以及 xbean、xerces、wss4j、mail、xmlsec、httpclient、jaxen、jdom 和 jetty 等一系列工具类库。这些库可能用于构建 SOAP Web 服务(webservice...