HttpClient使用笔记
2016-02-01 23:21
507 查看
四步走:
1、先创建一个DefaultHttpClient的实例
HttpClient httpClient=new DefaultHttpClient();
2、发送GET请求:
先创建一个HttpGet对象,传入目标的网络地址,然后调用HttpClient的execute()方法即可:
HttpGet HttpGet=new HttpGet(“http://www.baidu.com”);
httpClient.execute(httpGet);
3、返回响应实体↓
HttpResponse response
4、获取消息实体
HttpEntity entity = response.getEntity();
初步使用代码
参考资料
http://blog.csdn.net/manymore13/article/details/8481230 http://itindex.net/detail/52566-httpclient
1、先创建一个DefaultHttpClient的实例
HttpClient httpClient=new DefaultHttpClient();
2、发送GET请求:
先创建一个HttpGet对象,传入目标的网络地址,然后调用HttpClient的execute()方法即可:
HttpGet HttpGet=new HttpGet(“http://www.baidu.com”);
httpClient.execute(httpGet);
3、返回响应实体↓
HttpResponse response
4、获取消息实体
HttpEntity entity = response.getEntity();
初步使用代码
public class HttpCLientDemo { // HttpClient 代表Http客户端 // HttpEntity 消息载体,发送或者接收消息的载体,可以通过客户端请求或者服务器响应获取实例 // HttpConnection 代表http连接 /** * @param args */ public static void main(String[] args) { // 创建默认的客户端实例 HttpClient httpCLient = new DefaultHttpClient(); // 创建get请求实例 HttpGet httpget = new HttpGet("http://www.baidu.com"); System.out.println("executing request "+httpget.getURI()); try { // 客户端执行get请求 返回响应实体 HttpResponse response = httpCLient.execute(httpget); // 服务器响应状态行 System.out.println(response.getStatusLine()); Header[] heads = response.getAllHeaders(); // 打印所有响应头 for(Header h:heads){ System.out.println(h.getName()+":"+h.getValue()); } // 获取响应消息实体 HttpEntity entity = response.getEntity(); System.out.println("------------------------------------"); if(entity != null){ //响应内容 System.out.println(EntityUtils.toString(entity)); System.out.println("----------------------------------------"); // 响应内容长度 System.out.println("响应内容长度:"+entity.getContentLength()); } } catch (ClientProtocolException e){ e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); }finally{ httpCLient.getConnectionManager().shutdown(); } } }
参考资料
http://blog.csdn.net/manymore13/article/details/8481230 http://itindex.net/detail/52566-httpclient
相关文章推荐
- HTTP2.0那些事
- 网络OSI参考模型
- Broadleaf电商系统开发(五) - Http Api
- 全面解析4G LTE网络三大主流语音解决方案
- 在iOS9 中,苹果将原http协议改成了https协议,使用 TLS1.2 SSL加密请求数据。要使用HTTP要修改PTLIST
- 用Fiddler在Android上抓HTTPS包
- android 6.0(api 23) SDK,不再提供org.apache.http.*(只保留几个类)
- OTN交换&P-OTN有效减少100G网络成本(一)
- ssh使用http代理登录工具
- TCP三次握手四次挥手过程详解
- 图文介绍HTTP/2(特性、解决的问题、实现方法)
- HttpClient超时设置_demo
- 7.9 Models -- Connection to An HTTP Server
- http://www.cnblogs.com/fnng/archive/2011/12/02/2272610.html
- Fix The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 b
- 使用HttpURLConnection设置请求超时时间,请求超时时会导致自动重发。
- HTTP 错误 500.19 - Internal Server Error(Windows Server 2012)
- HttpWebRequest和HttpWebResponse类的认识
- 检测网络
- window下查看TCP端口连接情况