jetty client 与apache http client的实现、分析
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了jetty client 与apache http client的实现、分析,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3084字,纯文字阅读大概需要5分钟。
内容图文
谈到httpclient的话,只要会想到apache的httpclient和jetty的httpclient,但是apache的httpclient3和4之间又有区别,通过学些,最终总结了三种方式使用HttpClient,
分别为使用httpclient3,httpclient4,jetty的httpclient,下面分别来贴代码:
第1种:使用的jar包为commons-httpclient-3.1,只需要一个jar包即可
这里使用的是GetMethod,与httpcleint4有区别
public static void main(String[] args) { HttpClient httpClient = new HttpClient(); //打开窗口 GetMethod getMethod = new GetMethod("http://www.baidu.com/"); //输入网址 try { int statusCode = httpClient.executeMethod(getMethod); //按下回车运行,得到返回码 System.out.println(statusCode); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine()); } //读取内容 byte[] responseBody = getMethod.getResponseBody(); //得到返回的内容 //处理内容 System.out.println(new String(responseBody)); } catch (Exception e) { e.printStackTrace(); }finally{ getMethod.releaseConnection(); } }
第二种,使用的jar包为httpclient4,需要的jar包由httpclient3分成了多个,自己去下载
注意这里使用的是HttpGet,而httpclient3使用的是GetMethod
public static void main(String[] args) { HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("http://www.baidu.com"); // 打印请求信息 System.out.println(httpGet.getRequestLine()); try { // 发送请求,返回响应 HttpResponse response =httpClient.execute(httpGet); // 打印响应信息 System.out.println(response.getStatusLine()); HttpEntity httpEntity = response.getEntity(); System.out.println( httpEntity.getContentType()); System.out.println(httpEntity.getContentLength()); System.out.println(EntityUtils.getContentCharSet(httpEntity)); InputStream in = httpEntity.getContent(); //可以得到请求的内容了 ..... //这里可以自由发挥了 } catch (Exception e) { } }
第三种:使用jetty的httpclient,jar在jetty包里面的lib包里面
可以分为同步和异步两种
异步:
public static void main(String[] args) { HttpClient client = new HttpClient(); client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL); //还可以进行其他的配置 try { client.start(); } catch (Exception e) { e.printStackTrace(); } ContentExchange exchange = new ContentExchange() //exchange,里面放置回调方法 { protected void onResponseComplete() throws IOException { super.onResponseComplete(); String responseContent = this.getResponseContent(); System.out.println(responseContent); ........ //可以对返回结果自由发挥了 } }; exchange.setMethod("GET"); exchange.setURL("http://www.baidu.com"); try { client.send(exchange); //client发送消息 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
同步:
HttpClient httpClient = new HttpClient(); ...........................//同样对client进行配置 try { httpClient.start(); ContentExchange contentExchange = new ContentExchange(); httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL); contentExchange.setURL("http://www.baidu.com"); contentExchange.setTimeout(30000); httpClient.send(contentExchange); //client发送 contentExchange.waitForDone(); //同步等待结果返回 System.err.println("Response status: "+contentExchange.getResponseStatus()); System.out.println("Response content:"+contentExchange.getResponseContent()); } catch (Exception e) { e.printStackTrace(); }
希望对初学者有所帮助!!!!!
原文:http://5148737.blog.51cto.com/5138737/1413767
内容总结
以上是互联网集市为您收集整理的jetty client 与apache http client的实现、分析全部内容,希望文章能够帮你解决jetty client 与apache http client的实现、分析所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。